/* fafamt.p Fixed Assets Update                                             */
/* Copyright 1986 QAD Inc. All rights reserved.                             */
/* $Id:: fafamt.p 84895 2019-01-03 10:52:21Z aqe                           $: */
/* REVISION: 9.1      LAST MODIFIED: 08/26/99   BY: PJP *N021*              */
/* REVISION: 9.1      LAST MODIFIED: 01/20/00   BY: *N077* Vijaya Pakala    */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 05/17/00   BY: *M0LJ* Vihang Talwalkar */
/* REVISION: 9.1      LAST MODIFIED: 05/30/00   BY: *M0MY* Vihang Talwalkar */
/* REVISION: 9.1      LAST MODIFIED: 06/12/00   BY: *N0BX* Arul Victoria    */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0L0* Jacolyn Neder    */
/* REVISION: 9.1      LAST MODIFIED: 10/19/00   BY: *M0VF* Rajesh Lokre     */
/* REVISION: 9.1      LAST MODIFIED: 10/30/00   BY: *M0VM* Rajesh Lokre     */
/* REVISION: 9.1      LAST MODIFIED: 12/18/00   BY: *M0Y3* Veena Lad        */
/* REVISION: 9.1      LAST MODIFIED: 01/02/01   BY: *M0YM* Veena Lad        */
/* Revision: 1.31.1.23     BY: Alok Thacker     DATE: 03/13/02    ECO: *M1NB* */
/* Revision: 1.31.1.24     BY: Vinod Nair       DATE: 05/27/02    ECO: *N1K0* */
/* Revision: 1.31.1.27     BY: Kirti Desai      DATE: 07/16/02    ECO: *N1MJ* */
/* Revision: 1.31.1.28    BY: Deepali Kotavadekar DATE: 12/27/02  ECO: *N22M* */
/* Revision: 1.31.1.29     BY: Manish Dani      DATE: 01/03/03    ECO: *M1YW* */
/* Revision: 1.31.1.30     BY: Rajesh Lokre     DATE: 04/03/03    ECO: *M1RX* */
/* Revision: 1.31.1.32     BY: Paul Donnelly (SB) DATE: 06/26/03  ECO: *Q00C* */
/* Revision: 1.31.1.35     BY: Dorota Hohol     DATE: 10/20/03    ECO: *P138* */
/* Revision: 1.31.1.37     BY: Kirti Desai      DATE: 11/04/03    ECO: *N2M3* */
/* Revision: 1.31.1.38     BY: Robert Jensen    DATE: 09/02/04    ECO: *N2XK* */
/* Revision: 1.31.1.39     BY: Vivek Gogte      DATE: 09/30/04    ECO: *P2LV* */
/* Revision: 1.31.1.40     BY: Jignesh Rachh    DATE: 11/08/04    ECO: *P2T8* */
/* Revision: 1.31.1.41     BY: Sushant Pradhan  DATE: 09/22/05    ECO: *P421* */
/* Revision: 1.31.1.42     BY: cnl              DATE: 02/07/06    ECO: *P4H9* */
/* Revision: 1.31.1.43     BY: Sandeep Panchal  DATE: 03/14/06    ECO: *P4KC* */
/* Revision: 1.31.1.44     BY:  Dilip Manawat   DATE: 09/19/06    ECO: *P56R* */
/* Revision: 1.31.1.47     BY:  Deepak Taneja   DATE: 07/17/07    ECO: *P5P6* */
/* Revision: 1.31.1.48     BY: Anil Sudhakaran  DATE: 08/06/07    ECO: *R0C6* */
/* Revision: 1.31.1.49     BY: Jean Miller      DATE: 02/13/08    ECO: *R0NF* */
/* Revision: 1.31.1.50     BY: Jorge Rodriguez  DATE: 03/07/08    ECO: *R0NB* */
/* Revision: 1.31.1.55     BY: Dinesh Dubey     DATE: 03/18/08    ECO: *R0PX* */
/* Revision: 1.31.1.56     BY: Jorge Rodriguez  DATE: 04/16/08    ECO: *R0Q0* */
/* Revision: 1.19          BY: Dilip Manawat    DATE: 04/29/08    ECO: *R0S4* */
/* Revision: 1.31.1.61     BY: Diyin Zhou       DATE: 07/08/10    ECO: *R21B* */
/* REVISION: 23537     LAST MODIFIED: 10/19/12    BY: ddo                     */
/*-Revision end---------------------------------------------------------------*/
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
using com.qad.eefin.buser.EntitySecurityCheck.

{us/mf/mfdtitle.i}

{us/gp/gprunpdf.i "mcpl" "p"}

/* Common API Constants and Variables */
{us/mf/mfaimfg.i}

define variable l-acct-seq as integer format "999999999"
   initial 0 no-undo.

/* --------------------  DEFINE QUERY  ------------------- */
define query q_fa_mstr
   for fa_mstr
   scrolling.

/* -----------------  STANDARD VARIABLES  ---------------- */

define new shared variable l-detail             like mfc_logical no-undo.

define variable p-status             as character           no-undo.
define variable perform-status       as character           format "x(25)"
   initial "first" no-undo.
define variable p-skip-update        like mfc_logical no-undo.
define variable l-rowid              as rowid               no-undo.
define variable l-recid              as recid               no-undo.
define variable l-delete-it          like mfc_logical no-undo.
define variable l-del-rowid          as rowid               no-undo.
define variable l-top-rowid          as rowid               no-undo.
define variable lines                as integer             initial 10 no-undo.
define variable l-found              like mfc_logical no-undo.
define variable pos                  as integer             no-undo.
define variable l-first              like mfc_logical no-undo.
define variable l-error              like mfc_logical no-undo.
define variable l-title              as character           no-undo.

define variable l_amt_fmt            as character           no-undo.
define variable mc-error-number1     like msg_nbr no-undo.
define variable rndmthd              like rnd_rnd_mthd no-undo.

define variable l_postgl             like mfc_logical
   label "Post To GL" no-undo.
define variable l_printgl            like mfc_logical
   label "Print GL Audit Trail" no-undo.
define variable l_effdate            like glt_effdate
   format "99/99/9999" no-undo.
define variable l_old_acquis_effdate like fa_acquis_effectivedate
   format "99/99/9999" no-undo.
define variable l_astpost            like mfc_logical no-undo.
define variable l_cost               like fa_puramt no-undo.
define variable l_primbk_cost        like fa_puramt no-undo.
define variable l_post_bk            like fabk_id no-undo.
define variable l_ast_del            like fa_id no-undo.
define variable l_firstcost          like fa_puramt no-undo.
define variable l_cost_captured      like mfc_logical no-undo.
define variable l_newbk              like mfc_logical no-undo.
define variable l_orig_ent           like fabd_entity no-undo.
/* For calling QAD Financials Data Object to check GL Entity Security */
define variable hQADFinancialsLib    as handle              no-undo.
define variable EntitySecurity       as EntitySecurityCheck no-undo.
define variable valid_entity         like mfc_logical no-undo.
define variable BufErrorMessage      as character           no-undo.
define variable ErrorMessage         as character           no-undo.
/* ------------------  BUTTON VARIABLES  ----------------- */
define button b-update label "Update".
define button b-add    label "Add".
define button b-delete label "Delete".
define button b-option label "Option".
define button b-books  label "Books".
define button b-detail label "Detail".
define button b-find   label "Find".
define button b-end    label "End".

/* -------------  STANDARD WIDGET VARIABLES  ------------- */
define variable l-focus           as widget-handle no-undo.
define variable w-frame           as widget-handle no-undo.

/* ------------------  SCREEN VARIABLES  ----------------- */
define new shared variable l-key             as character     format "x(12)" no-undo.
define new shared variable l-reopen-fa-query like mfc_logical no-undo.
define new shared variable l-flag            like mfc_logical initial no no-undo.

define variable l_delta           like mfc_logical initial no no-undo.
define variable l_fabk_post       like mfc_logical initial no no-undo.
define variable l-post            like mfc_logical no-undo.
define variable l-gen             like mfc_logical no-undo.
define variable l-err-nbr         as integer       format "9999" no-undo.
define variable valid_acct        like mfc_logical initial no no-undo.
define variable fa_puramt_fmt     as character     format "x(30)" no-undo.
define variable fa_salvamt_fmt    as character     format "x(30)" no-undo.
define variable fa_replamt_fmt    as character     format "x(30)" no-undo.
define variable fa_dispamt_fmt    as character     format "x(30)" no-undo.
define variable l-first-time      like mfc_logical initial yes no-undo.
define variable l-continue        like mfc_logical no-undo.
define variable l-period          as character     format "x(6)" no-undo.
define variable l-cal             as character     format "x(8)" no-undo.
define variable l-fa-id           as character     format "x(12)"no-undo.
define variable l-temp-amt        as decimal       format "->>>>,>>>,>>9.99<<<<" no-undo.
define variable mc-error-number   as character     format "x(4)" no-undo.
define variable l_msg_list        as character     no-undo.
define variable l_sev_list        as character     no-undo.
define variable l_msg_arg1        as character     no-undo.
define variable l_msg_arg2        as character     no-undo.
define variable l_msg_arg3        as character     no-undo.
define variable l_msg_arg4        as character     no-undo.
define variable l_msg_arg5        as character     no-undo.
define variable fab_post_oriden   like mfc_logical no-undo.  
define variable l-is-delta-zero   as logical       no-undo.   

define variable l_old_puramt      like fa_puramt no-undo initial 0.
define variable l_cu_mstr         like cu_mstr.cu_curr no-undo.
define variable l_cu_mstr1        like cu_mstr.cu_curr no-undo.
define variable l_cu_mstr2        like cu_mstr.cu_curr no-undo.
define variable l_cu_mstr3        like cu_mstr.cu_curr no-undo.
define variable l_primary_curr    like currency.currencycode no-undo.
define variable l_primary_book    as character     no-undo.
define variable l_primary_override as logical no-undo.
define variable l-rnd-mthd        like gl_rnd_mthd no-undo.
define new shared temp-table dep no-undo like fabd_det.

define variable l_post_to_gl       as logical   initial no no-undo.
define variable l-quit             as logical   initial no no-undo.
define variable l-count            as integer   no-undo.
define variable l-book-currency    as character no-undo.
define variable l-message-text     as character initial "Can't update the Fixed asset" no-undo.
define variable l_validate_api_err as logical   no-undo.
define variable l_cost_acq         like fa_puramt no-undo.

/* Local variables for use in API mode */
define variable svFaQty            as decimal   no-undo.
define variable svFaId             as character no-undo.
define variable svFaClsId          as character no-undo.
define variable svFaLocId          as character no-undo.
define variable svFaDep            as logical   no-undo.
define variable svFaEntity         as character no-undo.
define variable svFaStart          as date      no-undo.
define variable svFaAcqEff         as date      no-undo.
define variable svFaPurAmt         as decimal   no-undo.
define variable svFaReplAmt        as decimal   no-undo.
define variable svFaSalvAmt        as decimal   no-undo.
define variable svFaAuthNbr        as character no-undo.

/* Checks to see if the API changed these values */
define variable origPurAmt         as decimal   no-undo.
define variable origSalvAmt        as decimal   no-undo.
define variable svFapurEntered     as logical   no-undo.
define variable svFaSalvEntered    as logical   no-undo.
define variable origLocId          as character no-undo.

define buffer bffab_det for fab_det.

/* ------------------  DESKTOP VARIABLE  ----------------- */
define variable dtokonly as logic initial yes no-undo.

/* ---------------  PRE PROCESSING INCLUDE  -------------- */

/* VARIABLES FOR GPGLEF1.I */
{us/gp/gpglefv.i}

/* VARIABLES FOR FA PERSISTENT PROCEDURES */
{us/gp/gprunpdf.i "fapl" "p"}

/* NRM SEQUENCE GENERATOR */
{us/gp/gpnbrgen.i}

/* VARIABLES FOR MC PROCEDURES */
{us/gp/gprunpdf.i "mcpl" "p"}

/* EAS PROCEDURES */
{us/gp/gprunpdf.i "gpglvpl" "p"}

/* DAYBOOK VARIABLES */
{us/gl/gldydef.i "new"}

/* NRM VARIABLES */
{us/gl/gldynrm.i "new"}

{us/gp/gprunpdf.i "nrm" "p"}

/* TEMP TABLE FOR AQUISITION COST POSTING LOGIC */
{us/fa/fapsmt.i "new"}

/* Fixed Asset API dataset definition */
{us/fa/fadsfamt.i "reference-only"}

/* ------------------  FRAME DEFINITION  ----------------- */
if c-application-mode <> "API"
then do:

   define frame f-button
      b-update at 1
      b-add    at 10
      b-option at 19
      b-books  at 28
      b-detail at 37
      b-find   at 46
      b-delete at 55
      b-end    at 64
   with no-box overlay three-d side-labels width 73.

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame f-button:handle).

   assign
      l-focus               = b-update:handle in frame f-button
      b-update:width        = 8
      b-update:private-data = "update"
      b-add:width           = 8
      b-add:private-data    = "add"
      b-delete:width        = 8
      b-delete:private-data = "delete"
      b-option:width        = 8
      b-option:private-data = "option"
      b-books:width         = 8
      b-books:private-data  = "books"
      b-detail:width        = 8
      b-detail:private-data = "detail"
      b-find:width          = 8
      b-find:private-data   = "find"
      b-end:width           = 8
      b-end:private-data    = "end".

   define frame f-1
      skip(.4)
      fa_id          colon 14
      fa_desc1 no-labels
      skip
      fa_facls_id    colon 17
      fa_qty         colon 67
      skip
      fa_faloc_id    colon 17
      fa_acq_post    colon 67
      skip
      fa_entity      colon 17
      fa_acquis_effectivedate  colon 67
      skip
      fa_dep         colon 17
      fa_startdt     colon 67
      skip
      fa_puramt      colon 17
      l_cu_mstr      no-labels
      fa_post        colon 67
      skip
      fa_salvamt     colon 17
      l_cu_mstr1     no-labels
      fa_auth_number colon 67
      skip
      fa_replamt     colon 17
      l_cu_mstr2     no-labels
      fa_disp_dt     colon 67
      skip
      fa_dispamt     colon 17
      l_cu_mstr3      no-labels
      fa_disp_rsn    colon 67
   with three-d side-labels width 80.
end.

/* Before we run the program, we should check if we can run it. */
/* because after we run the conversion or change the statutory  */
/* currency, we need to run the FA utility                      */
if can-find(first fact_ctrl where fact_domain = global_domain and fact_fa_enabled = no)
then do:
   {us/bbi/pxmsg.i &MSGNUM=13504 &ERRORLEVEL=4}
   {us/bbi/pxmsg.i &MSGNUM=13529 &ERRORLEVEL=4}
   return.
end.

/* get the primary book currency */
for first fabk_mstr no-lock
   where fabk_domain= global_domain and fabk_primary = yes,
   cu_mstr of fabk_mstr
no-lock:
   assign
      l_cu_mstr      = cu_mstr.cu_curr
      l_primary_curr = cu_mstr.cu_curr
      l_primary_book = fabk_id.
end.

l_amt_fmt = fa_puramt:format in frame f-1.
if l_primary_curr <> '' and l_primary_curr <> ?
then do:
   {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
      "(input l_primary_curr,
        output rndmthd,
        output mc-error-number1)"}
   if mc-error-number1 <> 0
   then	do:
      {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
   end.
   if rndmthd <> '' and rndmthd <>?
   then do:
      {us/bbi/gprun.i ""gpcurfmt.p""
         "(input-output l_amt_fmt,
           input rndmthd)"}
   end.
end.
if c-application-mode <> "API"
then do:
   assign
      fa_replamt:format in frame f-1 = l_amt_fmt
      fa_dispamt:format in frame f-1 = l_amt_fmt
      fa_salvamt:format in frame f-1 = l_amt_fmt
      fa_puramt :format in frame f-1 = l_amt_fmt.

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame f-1:handle).

   form
      skip(.4)
      l_postgl    colon 30
      l_effdate   colon 30
      l_printgl   colon 30
   with frame a overlay row 10 side-labels width 80.

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame a:handle).
message "run ip-framesetup" view-as alert-box.
   run ip-framesetup .

   w-frame = frame f-1:handle.

   {us/bbi/gprun.i ""fafmtut.p"" "(w-frame)"}
end. /* if c-application-mode <> "API" */

if c-application-mode = "API" then
do on error undo, return:

   /* Get Handle of API Controller */
   {us/bbi/gprun.i ""gpaigach.p"" "(output ApiMethodHandle)"}

   if not valid-handle(ApiMethodHandle)
   then do:
      /* API Error */
      {us/bbi/pxmsg.i &MSGNUM=10461 &ERRORLEVEL=4}
      return.
   end.

   /* Get the Fixed Asset API dataset from the controller */
   run getRequestDataset in APIMethodHandle
      (output dataset dsFixedAssetMaster bind).

   run validate-api-data(output l_validate_api_err).
   if l_validate_api_err = yes then return.

end.

/* GET THE POSTING BOOK */
for first fabk_mstr
   where fabk_mstr.fabk_domain = global_domain
    and  fabk_post = yes
    and  fabk_primary
no-lock:
l_post_bk = fabk_id.
end. /* FOR FIRST fabk_mstr */

for first fact_ctrl
   where fact_ctrl.fact_domain = global_domain
    and  fact_index1 = 0
no-lock
   use-index fact_index1:
end. /* FOR FIRST fact_ctrl */

if available fact_ctrl
then
   l-gen = fact_auto.
else do:
   /* CONTROL FILE ERROR. CHECK APPLICABLE CONTROL FILE EXISTENCE*/
   {us/bbi/pxmsg.i &MSGNUM=594 &ERRORLEVEL=3}.
   /* KEEP PAUSE TO HOLD THE MESSAGE - DON'T REMOVE */
   if c-application-mode <> "API" then
      pause.
   return.
end. /* ELSE DO */

/* -------------  END PRE PROCESSING INCLUDE  ------------ */

/* Don't need to open the query when in API mode */
if c-application-mode <> "API"
then do:
   open query q_fa_mstr
      for each fa_mstr
      where fa_mstr.fa_domain = global_domain and  fa_id >= ""
      use-index fa_id
   no-lock.

   get first q_fa_mstr no-lock.
end. /* c-application-mode <> "API" */
/* Get next record for the Fixed Asset Master */
else do:
   perform-status = "next".
end. /* c-application-mode = "API" */

message perform-status view-as alert-box.
main-loop:
do while perform-status <> "end" on error undo:
   if perform-status = "first"
   then
      assign
         p-status = "first-one".
         l_cost   = 0.
   /* CHECK FOR ENTITY SECURITY WHEN BUTTON 'UPDATE' 'OPTION' */
   /* 'BOOKS' 'DETAIL' OR 'DELETE' IS SELECTED                */
   l-error = no.

   if  perform-status <> "first"
      and perform-status <> "add"
      and perform-status <> "next"
      and perform-status <> "prev"
      and perform-status <> "find"
      and perform-status <> " "
      and available fa_mstr
   then do:
      /* CHECK ENTITY SECURITY */
      run valid-entity (input  fa_entity,
         output l-error).

      if l-error
      then
         if (batchrun or c-application-mode = "API")
         then
            return.
         else
            perform-status = " ".
      /* UOP method will be supported in termial model only. */
      if {us/bbi/gpiswrap.i} and not batchrun and c-application-mode <> "API"
      then do:
         DEFAULTBOOKS:
         for each fab_det where fab_domain = global_domain
              and fab_fa_id = fa_id
         no-lock:
            if can-find (first famt_mstr where famt_mstr.famt_domain = global_domain
               and   famt_id   = fab_famt_id
               and   famt_type = "2")
            then do:
               /* PROGRAM HAS LIMITED FUNCTIONALITY FOR UOP IN DESKTOP MODE */
               {us/bbi/pxmsg.i &MSGNUM=16103 &ERRORLEVEL=3}
               perform-status = " ".
               leave DEFAULTBOOKS.
            end.
         end.
      end.
   end. /* IF perform-status <> "first" ... */
   if perform-status = "first"
      and p-status = "first-one"
   then do:
      if available fa_mstr
      then
         {us/bbi/gprun.i ""fafamta.p""
            "(input        l_firstcost,
              input-output p-status,
              input-output l_newbk,
              input-output l_astpost,
              input-output l_cost,
              buffer       fa_mstr)"}
      p-status = "".
   end. /* IF perform-status = "first" and .... */
    message "2:" perform-status view-as alert-box.
   if perform-status = "end"
      or perform-status = "add"
   then do:
    message "1:end" perform-status view-as alert-box.
      p-status = perform-status.

      if perform-status = "add"
      then
         l_newbk = yes.

      if available fa_mstr
      then
         {us/bbi/gprun.i ""fafamta.p""
            "(input        l_firstcost,
              input-output p-status,
              input-output l_newbk,
              input-output l_astpost,
              input-output l_cost,
              buffer       fa_mstr)"}
      p-status = "".
   end. /* IF perform-status = "end" and .... */

   /* API Mode: ip-query will get record from ttFixedAssetMaster table;
   Change the perform-status to: add/update/delete/""/end;
   Get fa_mstr buffer;
   update fa_mstr base ttFixedAssetMaster; */
   run ip-query
      (input-output perform-status,
       input-output l-rowid).
 message "3:" perform-status view-as alert-box.
   if perform-status = "delete"
   then do:
      run ip-lock
         (input-output perform-status).

      perform-status = "first".
      run ip-predisplay.
   end. /* IF perform-status = "delete" and .... */

   /* ---------------  AFTER QUERY INCLUDE  --------------- */
   if l-first-time
      and available fa_mstr
      and fa_mstr.fa_dep
   then do:

      l-post = no.

      for each fab_det
      no-lock
         where fab_det.fab_domain = global_domain
          and  fab_fa_id = fa_id
         and   can-find(first fabk_mstr
         where fabk_domain = global_domain
         and   fabk_id   = fab_fabk_id
         and   fabk_post = yes):
         l-post = yes.
      end. /* FOR EACH fab_det .... */

      if l-post = no
      then do:
         for first fabk_mstr
            where fabk_mstr.fabk_domain = global_domain
            and  fabk_post = yes
         no-lock:
            /* POSTING BOOK NOT SET UP FOR THIS ASSET */
            {us/bbi/pxmsg.i &MSGNUM=4236 &ERRORLEVEL=2 &MSGARG1=fabk_id}
 
            l-first-time = no.
            next main-loop.
         end. /* FOR FIRST fabk_mstr */
      end. /* IF l-post = no */
   end.  /* IF FIRST TIME AND DEPRECIATING ASSET */
   /* -------------  END AFTER QUERY INCLUDE  ------------- */
   /* ----------------------  OPTION  --------------------- */
   if perform-status = "option"
   then do:
      p-status = "display".

      {us/bbi/gprun.i ""fafamtb.p""
         "(output l-flag)"}

      /* l-flag IS SET TO yes IN BATCH MODE IN PROGRAM fafamto.p */
      /* FOR AN ERROR ENCOUNTERED.                               */
      if l-flag
      then
         undo main-loop, leave main-loop.

      assign
         p-status       = ""
         perform-status = "first".
   message "ip-displayedits" view-as alert-box.
      run ip-displayedits.
      perform-status = "first".
   end. /* IF perform-status = "option" */
   /* --------------------  END OPTION  ------------------- */

   /* ----------------------  BOOKS  ---------------------- */
   if perform-status = "books" or
      (c-application-mode = "API" and (perform-status = "" or perform-status ="update")  and
      ttFixedAssetMaster.faButton = "books")
   then do:

      p-status = "display".

      if c-application-mode <> "API" then do:
         color display normal fa_id
         with frame f-1.
      end. /* c-application-mode <> "API" */

      if available fa_mstr then do:

         for first fab_det
         no-lock
            where fab_det.fab_domain = global_domain
             and  fab_fa_id = fa_id
            and can-find(first fabk_mstr
            where fabk_mstr.fabk_domain = global_domain
            and   fabk_id   = fab_fabk_id
            and   fabk_post = yes)
            and   l_cost_captured = no:

            assign
               l_firstcost     = fab_amt
               l_cost_captured = yes.
         end. /* FOR EACH fab_det */

         if l_newbk
         then
            l_firstcost = 0.

         /* INPUT-OUPUT PARAMETERS l_newbk l_astpost AND l_cost */
         {us/bbi/gprun.i ""fafamta.p""
            "(input        l_firstcost,
              input-output p-status,
              input-output l_newbk,
              input-output l_astpost,
              input-output l_cost,
              buffer       fa_mstr)"}
      end. /* IF AVAILABLE fa_mstr */

      /* l-flag IS SET TO yes IN BATCH MODE IN fafamta.p */
      /* FOR AN ERROR ENCOUNTERED.                       */
      if l-flag
      then
         undo main-loop, leave main-loop.

      if c-application-mode <> "API" 
      then
         assign
            p-status       = ""
            perform-status = "first".
      else
         perform-status = "end".

      run ip-displayedits.
   end.  /* IF perform-status = "books" */
   /* --------------------  END BOOKS  -------------------- */

   /* ----------------------  DETAIL  --------------------- */
   if perform-status = "detail"
   then do:
      p-status = "display".

      {us/bbi/gprun.i ""fafamtc.p""}

      assign
         p-status       = ""
         perform-status = "first".

      run ip-displayedits.
      perform-status = "first".
   end.  /* IF perform-status = "detail"  */
   /* --------------------  END DETAIL  ------------------- */

   /* ----------------------  DISPLAY  ---------------------- */
   message "ip-predisplay" view-as alert-box.
   run ip-predisplay.
   if (c-application-mode = "API" and perform-status = "end" and
      ttFixedAssetMaster.faButton = "books" and ttFixedAssetMaster.operation = {&MODIFY})
   then do:
      perform-status = "update".
   end.
   /* -------------------  ADD/UPDATE  -------------------- */
   if (perform-status = "update"
      or  perform-status = "add")
   then do:
      if available fa_mstr
      then
         l_old_puramt = fa_mstr.fa_puramt.
      else
         l_old_puramt = 0.

      run ip-prompt.
      if l_cost <> 0
      then
         if available fa_mstr then l_cost = fa_mstr.fa_puramt.
      if l_cost_captured = yes
         and l_newbk     = no
      then
         if available fa_mstr then l_cost = fa_mstr.fa_puramt - l_firstcost.
      /* l-flag IS SET TO yes IN BATCH MODE IN PROCEDURE ip-prompt */
      /* FOR AN ERROR ENCOUNTERED.                                 */
      if l-flag
      then
         undo main-loop, leave main-loop.

      if perform-status = "undo"
      then do:
         perform-status = "first".
         next main-loop.
      end. /* IF perform-status = "undo" */
      run ip-displayedits.
   end. /* IF perform-status = "update" AND ... */
   if (c-application-mode = "API" and
      ttFixedAssetMaster.faButton = "books" and ttFixedAssetMaster.operation = {&MODIFY})
   then do:
      perform-status = "end".
   end.
   /* -----------------------  END  ----------------------- */
   if perform-status = "end"
   then do:
       message "2:END" perform-status view-as alert-box.
      if c-application-mode <> "API" then
         hide frame f-1 no-pause.
      delete PROCEDURE this-procedure no-error.
      if c-application-mode <> "API" then
         leave.
   end. /* IF perform-status = "end" */

   /* ----------------  SELECTION BUTTONS  ---------------- */
   if perform-status <> "first"
   then do:
   message "ip-button" perform-status view-as alert-box.
      run ip-button
         (input-output perform-status).
 message "after ip-button" perform-status view-as alert-box.
      /* LOGIC FOR THE ACCEPTANCE AND PRINTING OF POST TO GL DATA */
      /* AND CREATION OF THE TEMP TABLE tt_pstast                 */
      if available fa_mstr
         and
         ( perform-status    = "add"
         or perform-status = "find"
         or perform-status = "next"
         or perform-status = "prev"
         or perform-status = "end"
         or lastkey = keycode("F4")
         )
         and (
         (c-application-mode ="API" and fa_mstr.fa_acq_post = yes and ttFixedAssetMaster.operation = {&MODIFY} ) or
         c-application-mode <> "API"
         )
      then do:
         if perform-status = "end" or lastkey = keycode("F4")
         then do:
          message "3:end" perform-status view-as alert-box.
            /* Validate fixed asset book detail exchange rata are input */
            for each fab_det
               where fab_det.fab_fa_id = fa_mstr.fa_id
                 and fab_det.fab_domain = global_domain:
               if fab_det.fab_bc_exr_rate = 0 or fab_det.fab_sc_exr_rate = 0
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=13714 &ERRORLEVEL=4 &MSGARG1 = fab_det.fab_fabk_id }
                  perform-status = "".
                  next main-loop.
               end.
            end.
         end.
         /* ACCEPT AND  STORE POST TO GL DATA */
         valid_acct = yes.

         run ip_posttogl.
         if valid_acct = no
         then do:
            perform-status = "".
            next main-loop.
         end.
         /* PRINT THE GL AUDIT TRAIL BASED ON USER INPUT */
         if perform-status = "end"
            or lastkey = keycode("F4") or (fa_acq_post = no or fa_posted_puramt <> fa_puramt)
         then do:
          message "4:end" perform-status view-as alert-box.
            find first tt_pstast
            no-lock no-error.
            if available tt_pstast
            then do:
               printloop:
               do on error undo, retry:
                  l_printgl = no.

                  {us/bbi/gprun.i ""fafamt1a.p""
                     "(input l_postgl,
                       input l_effdate,
                       input l_post_bk,
                       input l_printgl)"}

                  if c-application-mode <> "API" then
                     hide frame a.

                  leave printloop.
               end. /* PRINTLOOP */

            end. /* IF AVAILABLE tt_pstast */
         end. /* IF perform-status = "end" */

         assign
            l_astpost       = no
            l_cost          = 0
            l_cost_captured = no
            l_firstcost     = 0
            l_newbk         = no.

         if perform-status = "end"
            and not can-find(first tt_pstast)
         then
            return.

      end. /* IF perform-status = "add" ... */

   end. /* IF perform-status <> "first" */

   /* -------------  AFTER STRIP MENU INCLUDE  ------------ */
   if available fa_mstr
   then
      l-key = fa_id.

   if perform-status = "books"
      and c-application-mode <> "API"
      and not input frame f-1 fa_dep
   then do:
      /* BOOKS ARE NOT MAINTAINED FOR NON DEPRECIATING ASSETS  */
      {us/bbi/pxmsg.i &MSGNUM=3194 &ERRORLEVEL=4}

      perform-status = "".
      next main-loop.
   end. /* IF perform-status = "books" */

   if perform-status = "find" and c-application-mode <> "API"
   then do:
      repeat:
         prompt-for
            fa_id
         with frame f-1.
         l-error = no.
         for first fa_mstr
            where fa_mstr.fa_domain = global_domain
             and  fa_id >= input frame f-1 fa_id
         no-lock:
            l-rowid = rowid(fa_mstr).
            l_old_puramt = fa_puramt.
            l-key = fa_id.
         end. /* end for first fa_mstr */
         /* UOP method will be supported in termial model only. */
         if {us/bbi/gpiswrap.i} and not batchrun and c-application-mode <> "API"
         then do:
            DEFAULTBOOKS:
            for each fab_det where fab_domain = global_domain
                  and fab_fa_id = input frame f-1 fa_id
               no-lock:
               if can-find (first famt_mstr where famt_mstr.famt_domain = global_domain
                  and   famt_id   = fab_famt_id
                  and   famt_type = "2")
               then do:
                  l-error = yes.
                  /* PROGRAM HAS LIMITED FUNCTIONALITY FOR UOP IN DESKTOP MODE */
                  {us/bbi/pxmsg.i &MSGNUM=16103 &ERRORLEVEL=3}
                  leave DEFAULTBOOKS.
               end.
            end.
         end. /* end if {us/bbi/gpiswrap.i}  */
         if l-error
         then do:
            next-prompt
               fa_id
            with frame f-1.
            next.
         end.
         leave.
      end.

      run ip-query
         (input-output perform-status,
         input-output l-rowid).
      perform-status = "first".
   end. /* IF perform-status = "find" */
   /* re-assign l_cu_mstr by fab_det.fab_fabk_id */
   if available fa_mstr
   then do:
      for each fab_det
            where fab_det.fab_fa_id = fa_mstr.fa_id
              and fab_domain = global_domain no-lock,
            each fabk_mstr where fabk_domain = global_domain
             and fabk_id = fab_fabk_id no-lock
      break by fab_fabk_id:
         if first(fab_fabk_id) or fabk_primary
         then do:
            find cu_mstr where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr no-lock no-error.
            if available cu_mstr then l_cu_mstr = cu_mstr.cu_curr.
         end.
      end.
   end.

   if (perform-status = "end"
      or lastkey = keycode("F4")) and c-application-mode <> "API"
   then do:
    message "5:end " perform-status view-as alert-box.
      /* Validate fixed asset book detail exchange rata are input */
      for each fab_det where fab_det.fab_fa_id = fa_mstr.fa_id
           and fab_det.fab_domain = global_domain:
         if fab_det.fab_bc_exr_rate = 0 or fab_det.fab_sc_exr_rate = 0
         then do:
            {us/bbi/pxmsg.i &MSGNUM=594 &ERRORLEVEL=3}
            next main-loop.
         end.
      end.
   end.
   /* When output to PAGE in Desktop, have to wait here in order to finish report.  */
   if {us/bbi/gpiswrap.i} and (perform-status = "end") and c-application-mode <> "API"
   then do:
        message "6:end" perform-status view-as alert-box.
      /* Please confirm exit */
      {us/bbi/pxmsg.i &MSGNUM=36 &ERRORLEVEL=1 &CONFIRM=dtokonly &CONFIRM-TYPE='LOGICAL'}
   end.
   /* -----------  END AFTER STRIP MENU INCLUDE  ---------- */
end.  /* MAIN-LOOP: */


/* -------------  ADD / UPDATE / FIELD EDITS  ------------ */
PROCEDURE ip-prompt:
   if perform-status = "add" and c-application-mode <> "API" then
      clear frame f-1 all no-pause.

   l-first = yes.
   if c-application-mode <> "API"
   then do:
      CASE perform-status:
         when ("add")
         then do:
            /* ADDING NEW RECORD */
            {us/bbi/pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}
         end. /* WHEN ("add") */
         when ("update")
         then do:
            /* MODIFYING EXISTING RECORD */
            {us/bbi/pxmsg.i &MSGNUM=10 &ERRORLEVEL=1}
         end. /* WHEN ("update") */
         otherwise
            hide message no-pause.
      END CASE.

      ststatus = stline[3].
      status input ststatus.

      /* BEFORE-ADD-EDIT */
      if l-first
         and perform-status = "add"
      then
         fa_qty:screen-value = "1".

   end. /* c-application-mode <> "API" */
   else do:
      if ttFixedAssetMaster.faQty = 0 or ttFixedAssetMaster.faQty = ? then
         ttFixedAssetMaster.faQty = 1.
   end. /* c-application-mode = "API" */

   /* BEFORE-ADD-EDIT */
   l-post = if available fa_mstr
               and perform-status <> "add"
            then
               fa_mstr.fa_post
            else
               no.
   /* BEFORE-ADD-EDIT */ 
   if c-application-mode <> "API"   
   then do:   
      if perform-status = "add"
      then do:
         fa_acquis_effectivedate:screen-value = string(today).
      end.
   end.
   else {us/mf/mfaidflt.i ttFixedAssetMaster.faacquiseffectivedate today}.

   prompt-for-it:
   repeat:
      l-first = no.
      repeat:
         if c-application-mode <> "API" then
            prompt-for
               fa_mstr.fa_id       when (perform-status = "add" and l-gen = no)
               fa_mstr.fa_desc1
               fa_mstr.fa_facls_id
               fa_mstr.fa_faloc_id when (l-post = no)
            with frame f-1.
 
         /* FIELD EDIT FOR fa_id */
         l-error = no.

         /* Assign the local variables from either the UI or API */
         assign
            svFaId    = if c-application-mode <> "API" then
                           (input frame f-1 fa_id)
                        else
                           ttFixedAssetMaster.faId
            svFaClsId = if c-application-mode <> "API" then
                           (input frame f-1 fa_facls_id)
                        else
                           ttFixedAssetMaster.faFaclsId
            svFaLocId = if c-application-mode <> "API" then
                           (input frame f-1 fa_faloc_id)
                        else if l-post = no then
                           ttFixedAssetMaster.faFalocId
                        else 
                           fa_mstr.fa_faloc_id.

         /* Check if trying to add Fixed Asset which already exists */
         if l-gen = no
         then do:

            if (can-find(fa_mstr
               where fa_mstr.fa_domain = global_domain
                and  fa_id = svFAId
               no-lock)
               or can-find(fabchd_det
               where fabchd_det.fabchd_domain = global_domain
               and   fabchd_fa_id = svFAId
               no-lock))
               and perform-status = "add"
            then do:
               l-error = yes.
               /* DUPLICATE RECORD EXISTS */
               {us/bbi/pxmsg.i &MSGNUM=2041 &ERRORLEVEL=3}
            end. /* IF (CAN-FIND(fa_mstr .... */
         end.

         if svFaId = ""
            and l-gen                     = no
         then do:
            /* BLANK NOT ALLOWED */
            {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}
            l-error = yes.
         end. /* IF INPUT frame f-1 fa_mstr.fa_id .... */
         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt fa_id with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR fa_id */

         /* Class ID validation */
         if svFaClsId = ""
         then do:
            /* BLANK NOT ALLOWED */
            {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3 &FIELDNAME=""faFaclsId""}

            /* l-flag IS SET TO yes IN BATCH MODE. */
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt fa_facls_id with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF INPUT frame f-1 fa_facls_id = "" */

         /* UOP method will be supported in termial model only. */
         if {us/bbi/gpiswrap.i} and not batchrun and c-application-mode <> "API"
         then do:
            DEFAULTBOOKS:
            for each fadf_mstr where fadf_domain = global_domain
                 and fadf_facls_id = svFaClsId
            no-lock:
               if can-find (first famt_mstr where famt_mstr.famt_domain = global_domain
                                            and   famt_id   = fadf_famt_id
                                            and   famt_type = "2") 
               then do:
                  l-error = yes.
                  /* PROGRAM HAS LIMITED FUNCTIONALITY FOR UOP IN DESKTOP MODE */
                  {us/bbi/pxmsg.i &MSGNUM=16103 &ERRORLEVEL=3}
                  leave DEFAULTBOOKS.
               end.
            end.
         end.
         if l-error
         then do:
            next-prompt fa_facls_id with frame f-1.
            next.
         end.

         /* Location ID Validation */
         if svFaLocId = ""
         then do:
            /* BLANK NOT ALLOWED */
            {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3 &FIELDNAME=""faFalocId""}

            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt fa_faloc_id with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF INPUT frame f-1 fa_faloc_id = "" */

         /* FIELD EDIT FOR fa_facls_id */
         l-error = no.
         for first facls_mstr
            where facls_mstr.facls_domain = global_domain
            and   facls_id = svFaClsId
         no-lock:
         end. /* FOR FIRST facls_mstr */

         if not available facls_mstr
         then do:
            l-error = yes.
            /* INVALID CLASS CODE */
            {us/bbi/pxmsg.i &MSGNUM=4212 &ERRORLEVEL=3 &FIELDNAME=""faFaclsId""}
         end. /* IF NOT AVAILABLE facls_mstr */
         else do:
            /* re-assign l_cu_mstr by fa_mstr.fa_facls_id */
            for each facd_det where facd_domain = global_domain  
               and facd_facls_id    = facls_id no-lock,
               each fabk_mstr where fabk_domain = global_domain 
                and fabk_id   = facd_fabk_id no-lock 
            break by oid_facd_det:
               if first(oid_facd_det) or fabk_primary 
               then do:
                  find cu_mstr where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr no-lock no-error.
                  if available cu_mstr then l_cu_mstr = cu_mstr.cu_curr.
               end.
            end.
            if perform-status = "add"
            then do:
               if c-application-mode <> "API" then
                  assign l_cu_mstr:screen-value = l_cu_mstr
                         l_cu_mstr1:screen-value = l_cu_mstr
                         l_cu_mstr2:screen-value = l_cu_mstr
                         l_cu_mstr3:screen-value = l_cu_mstr
                         fa_dep:screen-value    = string(facls_dep).	
               else
                  assign
                     {us/mf/mfaidflt.i ttFixedAssetMaster.faDep facls_dep}.
            end.
         end.
         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fa_facls_id
               with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR fa_facls_id */

         /* FIELD EDIT FOR fa_faloc_id */
         l-error = no.
         for first faloc_mstr
            where faloc_mstr.faloc_domain = global_domain
            and  faloc_id = svFaLocId
         no-lock:
         end. /* FOR FIRST faloc_mstr */

         if perform-status = "add"
         then do:
            find first facd_det no-lock
               where facd_det.facd_domain = global_domain
               and   facd_facls_id        =  svFaClsId
               and   facd_acctype         <> "0"
               and   facd_acct            =  ""
            no-error.
            if available facd_det
            then do:
               find first fabk_mstr no-lock where fabk_domain = global_domain and
                  fabk_id = facd_fabk_id no-error.
               if available fabk_mstr and fabk_post
               then do:
                  /* VALID NON-BLANK ACCOUNT NUMBER REQUIRED */
                  {us/bbi/pxmsg.i &MSGNUM=5227 &ERRORLEVEL=2 &FIELDNAME=""faFalocId""}
               end.
            end. /* IF AVAILABLE facd_det */
         end. /* if perform-status = "add" */

         if available faloc_mstr
         then do:

            /* CHECK ENTITY SECURITY */
            run valid-entity (input  faloc_entity,
               output l-error).

            if l-error
            then do:

               if not batchrun and c-application-mode <> "API"
               then do:
                  next-prompt
                     fa_faloc_id
                  with frame f-1.
                  next.
               end. /* IF NOT batchrun */
               else do:
                  l-flag = yes.
                  return.
               end. /* ELSE DO */
            end. /* IF NOT l-error */

            svFaEntity = faloc_mstr.faloc_entity.
            if c-application-mode <> "API"
            then do:
               fa_mstr.fa_entity:screen-value = faloc_mstr.faloc_entity.
            end.
            /* VALIDATE ACCT/SUB/CC FOR EACH DEFAULT ACCOUNT */
            for each facd_det
               where facd_det.facd_domain = global_domain
               and   facd_facls_id = svFaClsId
               and   facd_acct     <> ""
            no-lock:
               find first fabk_mstr no-lock
                  where fabk_domain = global_domain
                  and fabk_id = facd_fabk_id no-error.
               if available fabk_mstr and fabk_post
               then do:
                  /* INITIALIZE SETTINGS */
                  {us/gp/gprunp.i "gpglvpl" "p" "initialize"}

                  /* SET PROJECT VERIFICATION TO NO */
                  {us/gp/gprunp.i "gpglvpl" "p" "set_proj_ver"
                     "(input false)"}

                  /* TURN OFF THE DISPLAY MESSAGES */
                  {us/gp/gprunp.i "gpglvpl" "p" "set_disp_msgs"
                     "(input false)"}
                  /* SET ENTITY VERIFICATION TO YES */
                  {us/gp/gprunp.i "gpglvpl" "p" "set_coa_restriction_ver" "(input true, input svFaEntity)"}
                   
                  /* ACCT/SUB/CC/PROJ VALIDATION */
                  {us/gp/gprunp.i "gpglvpl" "p" "validate_fullcode"
                     "(input  facd_acct,
                       input  faloc_sub,
                       input  faloc_cc,
                       input  """",
                       input  (""facd_acct"" + ""_"" + facd_det.facd_acctype),
                       output valid_acct)"}

                  /* GET THE ERROR MESSAGES FROM gpglvpl.p */
                  {us/gp/gprunp.i "gpglvpl" "p" "get_msgs"
                     "(output l_msg_list,
                       output l_sev_list)"}

                  /* GET ERROR MESSAGE ARGUMENTS FROM gpglvpl.p */
                  {us/gp/gprunp.i "gpglvpl" "p" "get_msg_args"
                     "(output l_msg_arg1,
                       output l_msg_arg2,
                       output l_msg_arg3,
                       output l_msg_arg4,
                       output l_msg_arg5)"}

                  /* SUPPRESS HARD ERRORS FROM gpglvpl.p */
                  if integer(l_sev_list)    = 3
                     or integer(l_sev_list) = 4
                  then
                     l_sev_list = "2".

                  if not valid_acct
                  then do:
                     {us/bbi/pxmsg.i &MSGNUM=integer(l_msg_list)
                        &ERRORLEVEL=integer(l_sev_list)
                        &MSGARG1=l_msg_arg1
                        &MSGARG2=l_msg_arg2
                        &MSGARG3=l_msg_arg3
                        &MSGARG4=l_msg_arg4
                        &MSGARG5=l_msg_arg5}
                     leave.
                  end. /* IF NOT valid_acct */
               end. /* IF AVAIL fabk_mstr */
            end.  /* FOR EACH facd_det */
         end.  /* IF AVAILABLE faloc_mstr */
         else do:
            l-error = yes.
            /* LOCATION DOES NOT EXIST */
            {us/bbi/pxmsg.i &MSGNUM=4220 &ERRORLEVEL=3 &FIELDNAME=""faFalocId""}
         end. /* ELSE DO */

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fa_faloc_id
               with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR fa_faloc_id */

         leave.
      end. /* REPEAT */
      if keyfunction(lastkey) = "end-error"
      then do:
         perform-status = "undo".
         return no-apply.
      end. /* IF KEYFUNCTION(lastkey) = "end-error" */

      repeat:

         if c-application-mode <> "API" then
            prompt-for
               fa_mstr.fa_dep
               when (perform-status = "add")
               fa_mstr.fa_puramt
               when (perform-status = "add")
               with frame f-1.
         else do:
            assign
               svFaDep    = ttFixedAssetMaster.FaDep
               svFaPuramt = ttFixedAssetMaster.faPurAmt.
         end.

         /* FIELD EDIT FOR fa_startdt */
         l-error = no.

         if not l-post
         then do:

            /* Set the default value */
            if c-application-mode = "API" then
               assign
                  {us/mf/mfaidflt.i ttFixedAssetMaster.faPurAmt 0}.               
            assign
               svFaPuramt = if c-application-mode <> "API" then
                                (input frame f-1 fa_puramt)
                             else
                                ttFixedAssetMaster.faPurAmt.

         end. /* IF NOT l-post */

         /* FIELD EDIT FOR fa_puramt */
         l-error = no.

         if svFaPurAmt = 0
            and available fa_mstr
            and fa_mstr.fa_post       = no
         then do:
            /* BLANK NOT ALLOWED */
            {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3 &FIELDNAME=""faPuramt""}
            l-error = yes.
         end. /* IF INPUT frame .... */

         if c-application-mode <> "API" and
            input frame f-1 fa_puramt entered and
            l-error = no
         then do:
            fa_replamt:screen-value = input frame f-1 fa_puramt.
         end.
         else 
         if c-application-mode = "API" and l-error = no
         then do:
            if ttFixedAssetMaster.faReplamt = 0 or ttFixedAssetMaster.faReplamt = ? then
               ttFixedAssetMaster.faReplamt = ttFixedAssetMaster.faPuramt.
         end.

         /* CHECK CURRENCY DEPENDENT ROUNDING FORMAT */
         {us/bbi/gprun.i ""gpcurval.p""
            "(input  svFaPurAmt,
              input  rndmthd,
              output l-err-nbr)"}

         if l-err-nbr <> 0
         then
            l-error = yes.

         l-err-nbr = 0.

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fa_puramt
               with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */

         if perform-status = "update"
         then do:
            l-continue = yes.

            if c-application-mode <> "API" and
               input frame f-1 fa_puramt entered
            then do:
               {us/bbi/pxmsg.i &MSGNUM=3349 &ERRORLEVEL=1 &CONFIRM=l-continue
                  &CONFIRM-TYPE='LOGICAL'}
            end. /* IF perform-status = "update" */
            if l-continue = no
            then do:
               perform-status = "first".
               return no-apply.
            end. /* IF l-continue = no */
         end.  /* IF perform-status = "update" */
         /* END FIELD EDIT FOR fa_puramt */

         leave.
      end. /* REPEAT */

      if keyfunction(lastkey) = "end-error"
      then
         next prompt-for-it.

      repeat:
         if c-application-mode <> "API"
         then do:
            l_old_acquis_effdate = input frame f-1 fa_acquis_effectivedate.

            prompt-for
               fa_mstr.fa_salvamt when (perform-status = "add")
               fa_mstr.fa_replamt
               fa_mstr.fa_qty     when (perform-status = "add")
               fa_mstr.fa_acquis_effectivedate
                                  when ((available fa_mstr and fa_acq_post  = no) or perform-status = "add")
               fa_mstr.fa_startdt when (l-post = no)
               fa_mstr.fa_auth_number
            with frame f-1.
         end.

         /* Set the API default values */
         if c-application-mode = "API" and perform-status = "add" then
            assign
               {us/mf/mfaidflt.i ttFixedAssetMaster.faSalvamt 0}
               {us/mf/mfaidflt.i ttFixedAssetMaster.faReplamt 0}
               {us/mf/mfaidflt.i ttFixedAssetMaster.faAuthNumber """"}
               {us/mf/mfaidflt.i ttFixedAssetMaster.faacquiseffectivedate today}.

         assign
            svFaSalvAmt = if c-application-mode <> "API" then
                             (input frame f-1 fa_salvamt)
                          else if l-post = no then
                             ttFixedAssetMaster.faSalvamt
                          else
                             fa_mstr.fa_salvamt

            svFaReplAmt = if c-application-mode <> "API" then
                             (input frame f-1 fa_replamt)
                          else
                             ttFixedAssetMaster.faReplAmt

            svFaAuthNbr = if c-application-mode <> "API" then
                             (input frame f-1 fa_auth_number)
                          else
                             ttFixedAssetMaster.faAuthNumber

            svFaQty     = if c-application-mode <> "API" then
                             (input frame f-1 fa_mstr.fa_qty)
                          else if perform-status = "add" then
                             ttFixedAssetMaster.FaQty
                          else
                             fa_mstr.fa_qty

            svFaStart     = if c-application-mode <> "API" then
                               (input frame f-1 fa_mstr.fa_startdt)
                            else if perform-status = "add" then
                               ttFixedAssetMaster.faStartdt
                            else
                               fa_mstr.fa_startdt
            svFaAcqEff   = if c-application-mode <> "API" then
                              (input frame f-1 fa_mstr.fa_acquis_effectivedate)
                           else 
                              ttFixedAssetMaster.faacquiseffectivedate
            svFaPuramt   = if c-application-mode <> "API" then
                              (input frame f-1 fa_puramt)
                           else
                              ttFixedAssetMaster.faPurAmt.

         /* FIELD EDIT FOR fa_salvamt */
         l-error = no.
         if svFaSalvamt > svFaPuramt
         then do:
            /* SALVAGE VALUE CANNOT BE GREATER THAN COST */
            {us/bbi/pxmsg.i &MSGNUM=3196 &ERRORLEVEL=3 &FIELDNAME=""faSalvamt""}
            l-error = yes.
         end. /* IF INPUT frame .... */

         if svFaSalvamt < 0
         then do:
            /* SALVAGE VALUE CANNOT BE LESS THAN ZERO */
            {us/bbi/pxmsg.i &MSGNUM=228 &ERRORLEVEL=3 &FIELDNAME=""faSalvamt""}
            l-error = yes.
         end. /* IF INPUT frame .... */

         /* CHECK CURRENCY DEPENDENT ROUNDING FORMAT */
         {us/bbi/gprun.i ""gpcurval.p""
            "(input  svFaSalvamt,
              input  rndmthd,
              output l-err-nbr)"}

         if l-err-nbr <> 0
            then
            l-error = yes.

         l-err-nbr = 0.

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fa_salvamt
               with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */

         if perform-status = "update"
         then do:
            l-continue = yes.

            if not input frame f-1 fa_puramt entered
               and input frame f-1 fa_salvamt entered
               and c-application-mode <> "API"
            then do:
               {us/bbi/pxmsg.i &MSGNUM=3350 &ERRORLEVEL=1 &CONFIRM=l-continue
                  &CONFIRM-TYPE='LOGICAL'}
            end. /* IF NOT INPUT frame .... */

            if l-continue = no
            then do:
               perform-status = "first".
               return no-apply.
            end. /* IF l-continue = no */
         end. /* IF perform-status = "update" */
         /* END FIELD EDIT FOR fa_salvamt */

         /* FIELD EDIT FOR fa_replamt */
         l-error = no.

         /* CHECK CURRENCY DEPENDENT ROUNDING FORMAT */
         {us/bbi/gprun.i ""gpcurval.p""
            "(input  svFaReplamt,
              input  rndmthd,
              output l-err-nbr)"}

         if l-err-nbr <> 0
         then
            l-error = yes.
         l-err-nbr = 0.

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fa_replamt
               with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR fa_replamt */

         /* FIELD EDIT FOR fa_qty */
         l-error = no.

         if perform-status = "add"
         then do:
            if svFaQty < 1
            then do:
               /* QUANITY MUST BE GREATER THAN 0 */
               {us/bbi/pxmsg.i &MSGNUM=3195 &ERRORLEVEL=3 &FIELDNAME=""faQty""}

               if c-application-mode <> "API" then
                  fa_qty:screen-value = "1".
               assign
                  svFaQty = 1
                  l-error = yes.
            end. /* IF INPUT FRAME .... */

            /* VALIDATE THAT NUMBER OF COMPONENTS DOES NOT */
            /* RESULT IN TOO SMALL OF A COMPONENT COST     */
            l-temp-amt = (svFaPurAmt / svFaQty) no-error.

            if l-temp-amt < 0.01
            then do:
               /* COST PER COMPONENT MUST BE GREATER THAN 0 */
               {us/bbi/pxmsg.i &MSGNUM=3221 &ERRORLEVEL=3 &FIELDNAME=""faQty""}
               l-error = yes.
            end. /* IF l-temp-amt < 0.01 */
         end.  /* IF perform-status = "add" */

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:
            if not batchrun and c-application-mode <> "API"
            then do:
               next-prompt
                  fa_qty
               with frame f-1.
               next.
            end. /* IF NOT batchrun */
            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR fa_qty */

         /* FIELD EDIT FOR fa_acquis_effectivedate */
         l-error = no.
         if perform-status = "add"
         then do:
            if svFaAcqEff = ?
            then do:
               /* DATE REQUIRED */
               {us/bbi/pxmsg.i &MSGNUM=711 &ERRORLEVEL=3}

               /* l-flag IS SET TO yes IN BATCH MODE. */
               if not batchrun and c-application-mode <> "API"
               then do:
                  next-prompt
                     fa_acquis_effectivedate
                  with frame f-1.
                  next.
               end. /* IF NOT batchrun */
               else do:
                  l-flag = yes.
                  return.
               end. /* ELSE batchrun */
            end. /* if input frame f-1 fa_acquis_effectivedate = ? */

            /* l-flag IS SET TO yes IN BATCH MODE. */
            if l-error
            then do:
               if not batchrun and c-application-mode <> "API"
               then do:
                  next-prompt
                     fa_acquis_effectivedate
                  with frame f-1.
                  next.
               end. /* IF NOT batchrun */
               else do:
                  l-flag = yes.
                  return.
               end. /* ELSE batchrun */
            end. /* IF l-error */
         end.

         if c-application-mode <> "API" 
         then do:
            if perform-status = "update" and l_old_acquis_effdate <> svFaAcqEff
            then do:
               {us/bbi/pxmsg.i &MSGNUM=13476 &MSGBUFFER = BufErrorMessage &ERRORLEVEL=1}
               ErrorMessage = BufErrorMessage.
               {us/bbi/pxmsg.i &MSGNUM=13488 &MSGBUFFER = BufErrorMessage &ERRORLEVEL=1}
               ErrorMessage = ErrorMessage + ". " + BufErrorMessage.
               {us/bbi/pxmsg.i &MSGTEXT = ErrorMessage &ERRORLEVEL=2}
            end.
         end.

         if svFaAcqEff <> ? 
         then do:
            {us/bbi/gprun.i ""gpglef1.p""
               "(input ""FA"",
                 input svFaEntity,
                 input svFaAcqEff,
                 output gpglef_result,
                 output gpglef_msg_nbr)"}

            if gpglef_result > 0
            then do:
               {us/bbi/pxmsg.i 
                  &MSGNUM=gpglef_msg_nbr 
                  &ERRORLEVEL=3
                  &MSGARG1=svFaEntity}

            if not batchrun 
               and c-application-mode <> "API"
            then do:
                  next-prompt
                     fa_acquis_effectivedate
                  with frame f-1.
                  next.
               end. /* IF NOT batchrun */
               else do:
                  l-flag = yes.
                  return.
               end. /* ELSE batchrun */
            end. /* IF gpglef_result > 0 */
         end. /* IF svFaAcqEff <> ? */
         /* END FIELD EDIT FOR fa_acquis_effectivedate */

         /* FIELD EDIT FOR fa_startdt */
         l-error = no.

         if not l-post
         then do:
            assign
               svFaStart  = if c-application-mode <> "API" then
                               (input frame f-1 fa_startdt)
                            else
                               ttFixedAssetMaster.faStartdt.
            if svFaStart = ? 
            then do:
               /* DATE REQUIRED */
               {us/bbi/pxmsg.i &MSGNUM=711 &ERRORLEVEL=3 &FIELDNAME=""faStartdt""}

               /* l-flag IS SET TO yes IN BATCH MODE. */
               if not batchrun and c-application-mode <> "API"
               then do:
                  next-prompt
                     fa_startdt
                  with frame f-1.
                  next.
               end. /* IF NOT batchrun */
               else do:
                  l-flag = yes.
                  return.
               end. /* ELSE batchrun */
            end. /* IF INPUT FRAME f-1 fa_startdt = ? */

            /* VALIDATE SERVICE DATE IN GL CALENDAR   */
            /* BUT ONLY DISPLAY A WARNING SINCE FA    */
            /* POST CAN PERFORM A "CATCH UP" FUNCTION */
            {us/bbi/gprun.i ""gpglef1.p""
               "(input  ""FA"",
                 input  svFaEntity,
                 input  svFaStart,
                 output gpglef_result,
                 output gpglef_msg_nbr)"}

            if gpglef_result = 2
            then do:
               /* CLOSED PERIOD */
               {us/bbi/pxmsg.i &MSGNUM=gpglef_msg_nbr &ERRORLEVEL=2 &FIELDNAME=""faStartdt""}
            end. /* IF gpglef_result > 1 */
            else 
            if gpglef_result <> 0
            then do:
               /* INVALID PERIOD OR INACTIVE ENTITY */
               {us/bbi/pxmsg.i &MSGNUM=gpglef_msg_nbr &ERRORLEVEL=3 &FIELDNAME=""faStartdt""}
               l-error = yes.
            end. /* IF gpglef_result = 2 */

            /* VALIDATE DATE IN CUSTOM CALENDAR IF ANY */
            for each fadf_mstr
               where fadf_mstr.fadf_domain = global_domain
               and   fadf_facls_id = svFaClsId
            no-lock:

               for first fabk_mstr
                  where fabk_mstr.fabk_domain = global_domain
                  and   fabk_id   =  fadf_fabk_id
                  and   fabk_calendar <> ""
                  and   fabk_post     =  no
               no-lock:
               end. /* FOR FIRST fabk_mstr */

               /* CUSTOM TABLE */
               for first famt_mstr
                  where famt_mstr.famt_domain = global_domain
                  and   famt_id   = fadf_famt_id
                  and   famt_type = "6"
               no-lock:

                  l-cal = if available fabk_mstr
                          then
                             fabk_calendar
                          else
                             "".

                  {us/gp/gprunp.i "fapl" "p" "fa-get-per"
                     "(input  svFaStart,
                       input  l-cal,
                       output l-period,
                       output l-err-nbr)"}

                  if l-err-nbr > 0
                  then do:
                     {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3 &FIELDNAME=""faStartdt""}
                     l-error = yes.
                     leave.
                  end. /* IF l-err-nbr > 0 */

                  if integer(substring(l-period,5,2)) > 13
                  then do:
                     /* CUSTOM TABLE CANNOT BE USED WITH A CALENDAR */
                     /* THAT HAS MORE THAN 13 PERIODS               */
                     {us/bbi/pxmsg.i &MSGNUM=3197 &ERRORLEVEL=3 &FIELDNAME=""faStartdt""}
                     l-error = yes.
                  end. /* IF INTEGER(SUBSTRING(l-period,5,2)) > 13 */
               end. /* FOR FIRST famt_mstr */
            end.  /* FOR EACH fadf_mstr */
         end. /* IF NOT l-post */

         /* l-flag IS SET TO yes IN BATCH MODE. */
         if l-error
         then do:

            if not batchrun and c-application-mode <> "API"
            then do:
               if gpglef_result = 3
               then do:
                  next-prompt
                     fa_faloc_id
                  with frame f-1.

                  next prompt-for-it.
               end.
               else do:

                  next-prompt
                     fa_startdt
                  with frame f-1.
                  next.
               end.

            end. /* if not batchrun and c-application-mode <> "API" */

            else do:
               l-flag = yes.
               return.
            end. /* ELSE batchrun */
         end. /* IF l-error */
         /* END FIELD EDIT FOR fa_startdt */

         leave.
      end. /* REPEAT */

      if keyfunction(lastkey) = "end-error"
      then
         next prompt-for-it.
      leave.
   end. /* prompt-for-it */

   if keyfunction(lastkey) = "end-error"
   then do:
      if perform-status = "add"
      then do:
         /* RECORD NOT ADDED */
         {us/bbi/pxmsg.i &MSGNUM=1304 &ERRORLEVEL=1}
      end.  /* IF perform-status = "add" */

      perform-status = "first".
      if c-application-mode <> "API" 
      then do:
         clear frame f-1.
         get current q_fa_mstr no-lock.
         run ip-displayedits.
      end.
      return.
   end. /* IF KEYFUNCTION(lastkey) = "end-error" */
   run ip-lock
      (input-output perform-status).
END PROCEDURE. /* ip-prompt */

/* -----------------------  Locking  ----------------------- */
PROCEDURE ip-lock:

   define input-output parameter perform-status as character no-undo.

   if perform-status    = "add"
      or perform-status = "update"
      or perform-status = "delete"
   then
   ip-lock:
   do transaction on error undo:
      assign
         pos         = 0
         l-delete-it = yes.

      if c-application-mode <> "API" 
      then do:
         if available fa_mstr
         then
            get current q_fa_mstr no-lock.

         if available fa_mstr and
            current-changed fa_mstr
         then do:
            l-delete-it = yes.
            /* RECORD HAS BEEN MODIFIED SINCED YOU EDITED IT SAVE ANYWAY */
            {us/bbi/pxmsg.i &MSGNUM=2042 &ERRORLEVEL=1 &CONFIRM=l-delete-it
               &CONFIRM-TYPE='LOGICAL'}

            if l-delete-it = no
            then do:
               hide message no-pause.
               run ip-displayedits.
               return.
            end. /* IF l-delete-it = no  */
            hide message no-pause.
         end. /* IF AVAILABLE fa_mstr .... */

      end.
      if available fa_mstr
      then
      tran-lock:

      do while perform-status <> "":
         if c-application-mode <> "API" then
            get current q_fa_mstr exclusive-lock no-wait.
         else
            find current fa_mstr exclusive-lock no-wait.

         if locked fa_mstr
         then do:
            if pos < 3
            then do:
               pos = pos + 1.
               pause 1 no-message.
               next tran-lock.
            end. /* IF pos < 3 */
            perform-status = "error".
         end. /* IF LOCKED fa_mstr */
         leave.
      end. /* DO WHILE */

      if (perform-status = "update"
         or p-status    = "update")
         and l-delete-it = yes
      then
         run ip-update
            (input-output perform-status).

      if perform-status = "delete"
      then 
         run ip-delete
            (input-output perform-status).

      if available fa_mstr and c-application-mode <> "API"
      then
         get current q_fa_mstr no-lock.
      else 
      if available fa_mstr then
         find current fa_mstr no-lock.

      if perform-status = "add"
      then
      run ip-add
         (input-output perform-status).

      if perform-status = "undo"
      then
         undo ip-lock, return.
   end. /* IF perform-status = "add" */

END PROCEDURE. /* ip-lock */

PROCEDURE ip-update:

   define input-output parameter perform-status as character no-undo.

   if p-status = "update"
   then
      p-status = "".

   /* LOCKED-UPDATE-EDITS */
   /* UPDATE CLASS ON DEPRECIATION ARRAY FROM THIS POINT FORWARD */

   for each fabd_det
      where fabd_det.fabd_domain = global_domain
      and   fabd_fa_id = fa_mstr.fa_id
   exclusive-lock:
      fabd_facls_id = svFaclsId.
   end. /* FOR EACH fabd_det */
   /* LOCKED-UPDATE-EDITS */

   run ip-assign
      (input-output perform-status).
   if perform-status = "undo"
   then
      return.

   if c-application-mode <> "API" 
   then do:
      perform-status = "open".
      run ip-query
         (input-output perform-status,
         input-output l-rowid).
   end. /* c-application-mode <> "API" */
   /* In API mode, process the Books next */
   else
      perform-status = "books".
END PROCEDURE. /* ip-update */

PROCEDURE ip-add:

   define input-output parameter perform-status as character no-undo.

   if l-gen
   then do:
      repeat:
         run getnbr
            (input  fact_ctrl.fact_seq_id,
             input  today,
             output l-fa-id,
             output l-error,
             output l-err-nbr).

         if l-error
         then do:
            {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=1}
            perform-status = "undo".
            return.
         end. /* IF l-error */

         if length(l-fa-id) > 12
         then do:
            /* ASSET ID IS MORE THAN 12 CHARACTERS LENGTH */
            {us/bbi/pxmsg.i &MSGNUM=3193 &ERRORLEVEL=3}
            return.
         end. /* IF LENGTH(l-fa-id) > 12 */

         if not can-find(first fa_mstr
            where fa_mstr.fa_domain = global_domain and  fa_id = l-fa-id)
            and not can-find(first fabchd_det
            where fabchd_det.fabchd_domain = global_domain
            and  fabchd_fa_id = l-fa-id)
         then
            leave.
      end. /* REPEAT */

      if c-application-mode <> "API" then
         fa_id:screen-value in frame f-1 = l-fa-id.
      else
         svFaId = l-fa-id.

   end. /* IF l-gen */

   create fa_mstr.
   fa_mstr.fa_domain = global_domain.
   run ip-assign
      (input-output perform-status).

   if recid(fa_mstr) = -1 then .

   if perform-status = "undo"
   then
      return.

   if c-application-mode <> "API" 
   then do:
      perform-status = "open".
      run ip-query
         (input-output perform-status,
          input-output l-rowid).
      perform-status = "first".
   end.

   /* In API mode, process the Books next */
   else
      perform-status = "books".

   return.
END PROCEDURE. /* ip-add */

PROCEDURE ip-delete:

   define input-output parameter perform-status as character no-undo.
   define variable l_acq_post like mfc_logical no-undo.

   l-delete-it = no.

   /* PLEASE CONFIRM DELETE */
   if c-application-mode <> "API" 
   then do:
      {us/bbi/pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=l-delete-it
         &CONFIRM-TYPE='LOGICAL'}
   end.
   else l-delete-it = yes.

   CASE l-delete-it:
      when yes
      then do:
         /* LOCKED-DELETE-EDIT */
         if fa_mstr.fa_post = yes
         then do:
            /* CANNOT DELETE - DEPRECIATION TAKEN FOR THIS ASSET */
            {us/bbi/pxmsg.i &MSGNUM=4226 &ERRORLEVEL=3}
            perform-status = "".
            return.
         end. /* if fa_post = yes */

         l_acq_post = no.
         if fa_acq_post 
         then do: 
            for each fab_det
               where fab_domain = global_domain
               and   fab_fa_id  = fa_mstr.fa_id
	       and   fab_resrv  = 0
               and   can-find(first fabk_mstr
               where fabk_domain  = global_domain
               and   fabk_id      = fab_fabk_id
               and   fabk_primary = yes
               and   fabk_post    = yes)
            no-lock:
               {us/gp/gprunp.i "fapl" "p" "fa-get-cost"
                 "(input  fab_fa_id,
                   input  fab_fabk_id,
                   output l_cost)"}
            end. /* FOR EACH fab_det */
    

            l_astpost = yes.

            if fa_posted_puramt <> 0
               or l_cost        <> 0
            then do:
               run ip_posttogl.

               if valid_acct = no
               then
                  return.    

               if lastkey = keycode("F4")
                  and not can-find(first tt_pstast)
               then
                  return.    

               /* PRINT THE GL AUDIT TRAIL BASED ON USER INPUT */
               for first tt_pstast
               no-lock:
                  printloop:
                  do on error undo, retry:
                     l_printgl = no.

                     {us/bbi/gprun.i ""fafamt1a.p""
                        "(input l_postgl,
                          input l_effdate,
                          input l_post_bk,
                          input l_printgl)"}      
                     if c-application-mode <> "API" 
                     then
                        hide frame a.

                     leave printloop.
                  end. /* PRINTLOOP */
               end. /* FOR FIRST tt_pstast */

               if lastkey = keycode("F4")
                  and can-find(first tt_pstast)
               then
                  return.
               l_acq_post = yes.
            end. /* IF fa_posted_puramt <> 0 OR l_cost <> 0 */
         end. /* IF fa_acq_post */
 
         /* DELETE ASSOCIATED RECORDS */
         {us/bbi/gprun.i ""fadebl.p""
            "(input fa_id,
              input yes)"}

         /* LOCKED-DELETE-EDIT */
         if c-application-mode <> "API" then
            hide message no-pause. 
         /* STORE DELETED ASSET CODE TO ENSURE THAT POSTING */
         /* DATA IF STORED IN tt_pstast GETS CLEARED        */
         assign
            l_ast_del = fa_id
            l_astpost = no.

         delete fa_mstr.
         if c-application-mode <> "API" 
         then do:
            clear frame f-1 no-pause.
            get next q_fa_mstr no-lock.
            if available fa_mstr
            then
               assign
                  perform-status = "first"
                  l-rowid        = rowid(fa_mstr).
            else do:
               get prev q_fa_mstr no-lock.
               if available fa_mstr
               then
                  assign
                     perform-status = "first"
                     l-rowid        = rowid(fa_mstr).
               else
                  assign
                     perform-status = "first"
                     l-rowid        = rowid(fa_mstr).
            end. /* ELSE DO */

         end. /* if c-application-mode */
         else
            perform-status = "next".

         if l_acq_post
         then do:
            if not batchrun
            then do:
               /* ASSET # DELETED. ACQUISITION TRANSACTION REVERSED */
               {us/bbi/pxmsg.i &MSGNUM=14728 &ERRORLEVEL=1 &MSGARG1=l_ast_del}

               if not {us/bbi/gpiswrap.i}
               then
                  pause 1.
            end. /* IF NOT batchrun */      
         end. /* IF fa_acq_post */
         else do:
            /* RECORD DELETED */
            {us/bbi/pxmsg.i &MSGNUM=22 &ERRORLEVEL=1}
         end. /* IF fa_acq_post = NO */
         return.   
      end.
      otherwise
         if c-application-mode <> "API" then
            run ip-displayedits.
   END CASE.
END PROCEDURE. /* ip-delete */

PROCEDURE ip-assign:

   define input-output parameter perform-status as character no-undo.

   do with frame f-1:

      if c-application-mode <> "API" 
      then do:
         assign
            fa_mstr.fa_id
            fa_mstr.fa_desc1
            fa_mstr.fa_faloc_id
            fa_mstr.fa_startdt
            fa_mstr.fa_puramt
            fa_mstr.fa_salvamt
            fa_mstr.fa_replamt
            fa_mstr.fa_acquis_effectivedate
            fa_mstr.fa_mod_userid = global_userid
            fa_mstr.fa_mod_date   = today
            fa_mstr.fa_auth_number
            fa_mstr.fa_dep
            fa_mstr.fa_qty
            fa_mstr.fa_facls_id.
      end.
      else do:
         assign
            fa_mstr.fa_id                   = svFaId
            fa_mstr.fa_desc1                = ttFixedAssetMaster.faDesc1
            fa_mstr.fa_faloc_id             = svFaLocId
            fa_mstr.fa_startdt              = svFaStart
            fa_mstr.fa_puramt               = svFaPurAmt
            fa_mstr.fa_salvamt              = svFaSalvAmt
            fa_mstr.fa_replamt              = svFaReplAmt
            fa_mstr.fa_mod_userid           = global_userid
            fa_mstr.fa_mod_date             = today
            fa_mstr.fa_auth_number          = svFaAuthNbr
            fa_mstr.fa_dep                  = svFaDep
            fa_mstr.fa_qty                  = svFaQty
            fa_mstr.fa_facls_id             = svFaClsId
            fa_mstr.fa_acquis_effectivedate = svFaAcqEff.
      end.
      /* FOR API, modifying the fixed asset, it doesn't need to update books detail later.
         since it already is processed in book detail level. except for the fixed asset has
         only one primary book with base currency.
      */
      find first ttFixedAssetBooks no-lock no-error.
      if c-application-mode = "API" and ttFixedAssetMaster.operation = {&MODIFY}
         and available ttFixedAssetBooks 
      then
         return.
      l-rowid = rowid(fa_mstr).

      {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
         "(input l_primary_curr,
           output rndmthd,
           output mc-error-number1)"}
      if mc-error-number1 <> 0 
      then do:
         {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
      end.

      {us/bbi/gprun.i ""gpcurfmt.p""
         "(input-output l_amt_fmt, input rndmthd)"}

      if c-application-mode <> "API" then
      assign
         fa_mstr.fa_puramt:format in frame f-1  = l_amt_fmt
         fa_mstr.fa_replamt:format in frame f-1 = l_amt_fmt
         fa_mstr.fa_salvamt:format in frame f-1 = l_amt_fmt.

      if c-application-mode = "API" then
         l-recid = recid(fa_mstr).
      
      /* --------  /* AFTER-ASSIGN-AUDIT-INCLUDE */  ----------- */
      fa_entity = svFaEntity.
      if  (perform-status = "add"
         or perform-status = "update")
      then do:
         {us/bbi/gprun.i ""fadpblb.p""
            "(buffer fa_mstr,
              """",
              perform-status,
              0,
              0,
              """",
              output l-err-nbr)"}

         /* RETURNS YES IF POSTING BOOK IS ATTACHED TO THE CLASS */
         {us/gp/gprunp.i "fapl" "p" "fa-get-post-class"
            "(input  input frame f-1 fa_facls_id,
              output l_astpost)"}
         if l_astpost and fa_puramt entered
         then
            l_cost = fa_puramt.
         if l-err-nbr <> 0
         then do:
            {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3}
            perform-status = "undo".
         end. /* IF l-err-nbr <> 0 */
      end. /* IF perform-status = "add" or perform-status = "update" */

      if (c-application-mode <> "API" and fa_puramt entered) or
         (c-application-mode = "API" and svFaPurAmt <> origPurAmt) 
      then
         svFapurEntered      = yes.
      if (c-application-mode <> "API" and fa_salvamt entered) or
         (c-application-mode = "API" and svFaSalvAmt <> origSalvAmt) 
      then
         svFaSalvEntered      = yes.

      if perform-status = "update"
         and ((c-application-mode <> "API" and (fa_puramt entered
               or fa_salvamt entered)) or
              (c-application-mode = "API" and (svFaPurAmt <> origPurAmt
               or svFaSalvAmt <> origSalvAmt)))
      then do:

         if l_newbk = no
         then do:
            for first fab_det
               where fab_det.fab_domain = global_domain
               and   fab_fa_id          = fa_id
               and   can-find(first fabk_mstr
               where fabk_mstr.fabk_domain = global_domain
               and   fabk_id               = fab_fabk_id
               and   fabk_post             = yes)
               and   l_cost_captured    = no
            no-lock:
               assign
                  l_firstcost     = fab_amt
                  l_cost_captured = yes.
            end. /* FOR EACH fab_det */
         end.  /* IF l_newbk = no */

         for first fab_det
            where fab_det.fab_domain  = global_domain
            and   fab_det.fab_fa_id   = fa_id
            and   fab_det.fab_fabk_id = l_post_bk
         exclusive-lock:
         end.

         if available fab_det
         then do:
            assign
               l_cost = if svFapurEntered
                        then fa_puramt - fab_amt
                        else 0.
            if c-application-mode <> "API" then
            assign
               fab_amt     = if fa_puramt entered
                             then fa_puramt
                             else fab_amt
               fab_salvamt = if fa_salvamt entered
                             then fa_salvamt
                             else fab_salvamt.
            else
            assign
               fab_amt     = if svFaPurAmt <> origPurAmt
                             then svFaPurAmt
                             else fab_amt
               fab_salvamt = if svFaSalvAmt <> origSalvAmt
                             then svFaSalvAmt
                             else fab_salvamt.

            if recid(fab_det) = -1 then .

            run ip-headercost
               (buffer fa_mstr,
                buffer fab_det,
                input svFapurEntered,
                input svFaSalvEntered,
                input l_cost,
                input fab_det.fab_fa_id,
                input fab_det.fab_fabk_id,
                input fab_det.fab_resrv,
                input l_post_bk,
                input l_delta,
                output perform-status,
                output l-err-nbr,
                input-output l_primbk_cost).

         end. /* IF AVAILABLE fab_det */
         if (c-application-mode <> "API" and (fa_puramt entered
            or fa_salvamt entered)) or
            (c-application-mode = "API" and (svFaPurAmt <> origPurAmt
            or svFaSalvAmt <> origSalvAmt))
         then do:

            for each fab_det
                  where fab_det.fab_domain  =  global_domain
                  and   fab_det.fab_fa_id   =  fa_id
                  and   fab_det.fab_fabk_id <> l_post_bk
            exclusive-lock:
               assign
                  l_cost = if svFapurEntered
                           then fa_puramt - fab_amt
                           else 0.
               if c-application-mode <> "API" then
               assign
                  fab_amt     = if fa_puramt entered
                                then fa_puramt
                                else fab_amt
                  fab_salvamt = if fa_salvamt entered
                                then fa_salvamt
                                else fab_salvamt.
               else
               assign
                  fab_amt     = if svFaPurAmt <> origPurAmt
                                then svFaPurAmt
                                else fab_amt
                  fab_salvamt = if svFaSalvAmt <> origSalvAmt
                                then svFaSalvAmt
                                else fab_salvamt.

               if recid(fab_det) = -1 then .

               run ip-headercost
                  (buffer fa_mstr,
                   buffer fab_det,
                   input svFapurEntered,
                   input svFaSalvEntered,
                   input l_cost,
                   input fab_det.fab_fa_id,
                   input fab_det.fab_fabk_id,
                   input fab_det.fab_resrv,
                   input l_post_bk,
                   input l_delta,
                   output perform-status,
                   output l-err-nbr,
                   input-output l_primbk_cost).
            end. /* FOR EACH fab_det */
            l_primbk_cost = 0.
         end. /* IF fa_puramt entered */

         if (c-application-mode <> "API" and fa_puramt entered) or
            (c-application-mode = "API" and svFaPurAmt <> origPurAmt)
         then do:
            {us/bbi/gprun.i ""faadbl.p""
               "(buffer fa_mstr)"}

            p-status = "first-one".

            {us/bbi/gprun.i ""fafamta.p""
               "(input        l_firstcost,
                 input-output p-status,
                 input-output l_newbk,
                 input-output l_astpost,
                 input-output l_cost,
                 buffer fa_mstr)"}
            /* RETURNS YES IF POSTING BOOK IS ATTACHED TO THE CLASS */
            {us/gp/gprunp.i "fapl" "p" "fa-get-post-class"
               "(input  input frame f-1 fa_facls_id,
                 output l_astpost)"}
            if l_astpost
            then
               l_cost = fa_puramt - l_firstcost.
            p-status = "".
         end.  /* IF fab_amt ENTERED */
      end. /* IF perform-status = "update" .... */

      else
      if perform-status = "update"
         and ((c-application-mode  <> "API"  
         and   fa_faloc_id entered)
         or   (c-application-mode  = "API"   
         and   svFaLocId           <> origLocId)) 
         or  l_orig_ent           <> svFaEntity
      then do:

         for each fabd_det
               where fabd_det.fabd_domain = global_domain
               and  fabd_fa_id = fa_id
         exclusive-lock:
            assign
               fabd_faloc_id = fa_faloc_id
               fabd_entity   = fa_entity.
         end. /* FOR EACH fabd_det */

      end. /* IF perform-status = "update" */

      /* ------  END AFTER-ASSIGN-AUDIT-INCLUDE  ----------- */
   end. /* DO WITH frame f-1 */
END PROCEDURE. /* ip-assign */

PROCEDURE ip-predisplay:

   if perform-status = ""
      and available fa_mstr
   then
   display-loop:
   do:
      if c-application-mode <> "API" then
         clear frame f-1 all no-pause.
      run ip-displayedits.
      if c-application-mode <> "API" then
         run ip-postdisplay.
   end. /* DO: */

END PROCEDURE. /* ip-predisplay */

PROCEDURE ip-displayedits:

   if available fa_mstr
   then do:

      if c-application-mode <> "API"
      then do:
         /* DISPLAY-EDITS */
         if l-reopen-fa-query = yes
         then do:

            /* RE-OPEN QUERY TO INCLUDE NEWLY CREATED ASSETS */
            perform-status = "open".
            run ip-query
               (input-output perform-status,
               input-output l-rowid).
         end. /* IF l-reopen-fa-query = yes */

      end. /* c-application-mode <> "API" */

      {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
         "(input l_primary_curr,
           output rndmthd,
           output mc-error-number1)"}

      if mc-error-number1 <> 0
      then do:
         {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
      end.

      {us/bbi/gprun.i ""gpcurfmt.p""
         "(input-output l_amt_fmt, input rndmthd)"}

      if c-application-mode <> "API" then
         assign
            fa_replamt:format in frame f-1 = l_amt_fmt
            fa_dispamt:format in frame f-1 = l_amt_fmt 
            fa_salvamt:format in frame f-1 = l_amt_fmt
            fa_puramt :format in frame f-1 = l_amt_fmt.
 
      run ip-display.
      l-reopen-fa-query = no.

   end. /* IF AVAILABLE fa_mstr */

END PROCEDURE. /* ip-displayedits */

PROCEDURE ip-display:
   l_old_puramt = fa_mstr.fa_puramt.
   l_cu_mstr1   = l_cu_mstr.
   l_cu_mstr2   = l_cu_mstr.
   l_cu_mstr3   = l_cu_mstr.

   if c-application-mode <> "API"
   then do:
      display
         fa_mstr.fa_id
         fa_mstr.fa_desc1
         fa_mstr.fa_faloc_id
         fa_mstr.fa_post
         fa_mstr.fa_startdt
         fa_mstr.fa_puramt
         fa_mstr.fa_salvamt
         fa_mstr.fa_disp_dt
         fa_mstr.fa_replamt
         l_cu_mstr
         fa_mstr.fa_acquis_effectivedate
         fa_mstr.fa_disp_rsn
         fa_mstr.fa_dispamt
         fa_mstr.fa_entity
         fa_mstr.fa_auth_number
         fa_mstr.fa_dep
         fa_mstr.fa_qty
         fa_mstr.fa_facls_id
         fa_mstr.fa_acq_post
         l_cu_mstr1
         l_cu_mstr2
         l_cu_mstr3
      with frame f-1.
   end. /* c-application-mode <> "API" */
END PROCEDURE. /* ip-postdisplay */

PROCEDURE ip-query:

   define input-output parameter perform-status as character no-undo.
   define input-output parameter l-rowid        as rowid     no-undo.

   /* API mode doesn't do scrolling */
   if c-application-mode = "API"
   then do:
      if perform-status = "first" or
         perform-status = "open" or
         perform-status = "prev" 
      then
         perform-status = "next".
   end. /* c-application-mode = "API" */

   if perform-status = "first"
   then do:

      if l-rowid <> ?
      then do:

         reposition q_fa_mstr to rowid l-rowid
            no-error.

         get next q_fa_mstr no-lock.
      end.  /* IF l-rowid <> ?  */

      if not available fa_mstr
      then
         get first q_fa_mstr no-lock.

      if available fa_mstr
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fa_mstr).
      else do:
         assign
            perform-status    = ""
            l-rowid           = ?
            frame f-1:visible = true.
         return.
      end. /* ELSE DO */
   end. /* IF perform-status = "first" */

   if perform-status = "open"
   then do:
      open query q_fa_mstr
         for each fa_mstr
         where fa_mstr.fa_domain = global_domain and  fa_id >= ""
         use-index fa_id
      no-lock.

      reposition q_fa_mstr to rowid l-rowid
         no-error.
      get next q_fa_mstr no-lock.

      if available fa_mstr
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fa_mstr).
      else do:
         get first q_fa_mstr no-lock.
         if not available fa_mstr
         then do:
            assign
               perform-status    = ""
               frame f-1:visible = true.
            return.
         end. /* IF NOT AVAILABLE fa_mstr */
         else
            assign
               perform-status = ""
               l-rowid        = rowid(fa_mstr).
      end. /* ELSE DO */
   end. /* IF perform-status = "open"  */

   if perform-status = "next"
   then do:

      if c-application-mode <> "API"
      then do:

         get next q_fa_mstr no-lock.
         if available fa_mstr
         then do:
            assign
               l-rowid        = rowid(fa_mstr)
               perform-status = "first".
            hide message no-pause.
            run ip-displayedits.
         end. /* IF AVAILABLE fa_mstr */
         else do:
            perform-status = "".
            /* END OF FILE */
            {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2}
            get prev q_fa_mstr no-lock.
         end. /* ELSE DO */
      end. /* c-application-mode <> "API" */
      else do:

         /* Get the Fixed Asset Master record from the API controller */
         run getNextRecord in ApiMethodHandle
            (input "ttFixedAssetMaster").

         if return-value = {&RECORD-NOT-FOUND}
         then do:
            assign
               perform-status = "end"
               l-recid        = ?.
            return.
         end.
         find first ttFixedAssetMaster exclusive-lock no-error.

         if ttFixedAssetMaster.operation = {&ADD} then
            perform-status = "add".
         else 
         if ttFixedAssetMaster.operation = {&MODIFY} then
            perform-status = "update".
         else 
         if ttFixedAssetMaster.operation = {&REMOVE} then
            perform-status = "delete".
         else 
         if ttFixedAssetMaster.operation = {&GET} then
            perform-status = "".
         else
            perform-status = "end".

         find first fa_mstr where fa_domain = global_domain
            and fa_id     = ttFixedAssetMaster.faid
         no-lock no-error.
         if ttFixedAssetMaster.operation = {&MODIFY} and available fa_mstr
         then do:
            assign 
               ttFixedAssetMaster.faQty = fa_mstr.fa_qty
               ttFixedAssetMaster.faDep = fa_mstr.fa_dep.
            if fa_mstr.fa_acq_post or ttFixedAssetMaster.faSalvamt = 0 or ttFixedAssetMaster.faSalvamt = ? then
               ttFixedAssetMaster.faSalvamt = fa_mstr.fa_salvamt.
            if ttFixedAssetMaster.faFalocId = "" or ttFixedAssetMaster.faFalocId = ? then
               ttFixedAssetMaster.faFalocId = fa_mstr.fa_faloc_id.
            if ttFixedAssetMaster.faPuramt = 0 or ttFixedAssetMaster.faPuramt = ? then
               ttFixedAssetMaster.faPuramt = fa_mstr.fa_puramt.
            if ttFixedAssetMaster.faAuthNumber = "" or ttFixedAssetMaster.faAuthNumber = ? then
               ttFixedAssetMaster.faAuthNumber = fa_mstr.fa_auth_number.
            if ttFixedAssetMaster.faFaclsId = "" or ttFixedAssetMaster.faFaclsId = ? then
               ttFixedAssetMaster.faFaclsId = fa_mstr.fa_facls_id.
            if ttFixedAssetMaster.faDesc1 = "" or ttFixedAssetMaster.faDesc1 = ? then
               ttFixedAssetMaster.faDesc1 = fa_mstr.fa_desc1.
            if ttFixedAssetMaster.faReplamt = 0 or ttFixedAssetMaster.faReplamt = ? then
               ttFixedAssetMaster.faReplamt = fa_mstr.fa_replamt.
            {us/mf/mfaidflt.i ttFixedAssetMaster.faStartdt    fa_mstr.fa_startdt}.
            if fa_mstr.fa_acq_post or ttFixedAssetMaster.faacquiseffectivedate = ? then
               ttFixedAssetMaster.faacquiseffectivedate =  fa_mstr.fa_acquis_effectivedate.
         end.

         run ip-displayedits.

      end. /* c-application-mode = "API" */
   end. /* IF perform-status = "next" */

   if perform-status = "prev"
   then do:

      get prev q_fa_mstr no-lock.
      if available fa_mstr
      then do:
         assign
            l-rowid        = rowid(fa_mstr)
            perform-status = "first".
         hide message no-pause.
         run ip-displayedits.

      end. /* IF AVAILABLE fa_mstr .... */
      else do:

         perform-status = "".
         /* BEGINNING OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2}
         get next q_fa_mstr no-lock.
      end. /* ELSE DO */

   end. /* IF perform-status = "prev" */

   if perform-status = "update"
      or perform-status = "delete"
      or (perform-status = "" and c-application-mode = "API")
   then do:

      if c-application-mode <> "API"
      then do:

         get current q_fa_mstr
         no-lock.
         if not available fa_mstr
         then do:
            hide message no-pause.
            /* RECORD NOT FOUND */
            {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
            perform-status = "".
         end. /* IF NOT AVAILABLE fa_mstr */
      end. /* c-application-mode <> "API" */
      else do:

         find fa_mstr where fa_mstr.fa_domain = global_domain
            and fa_mstr.fa_id = ttFixedAssetMaster.faId
         no-lock no-error.

         if not available fa_mstr
         then do:
            /* RECORD NOT FOUND */
            {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
            perform-status = "end".
         end. /* IF NOT AVAILABLE fa_mstr */
         else do:
            assign
               origPurAmt  = fa_mstr.fa_puramt
               origSalvAmt = fa_mstr.fa_salvamt
               origLocId   = fa_mstr.fa_faloc_id
               l_orig_ent  = fa_mstr.fa_entity.
         end.
      end. /* c-application-mode = "API" */
   end. /* IF perform-status = "update" */

END PROCEDURE. /* ip-query */

PROCEDURE ip-framesetup:

   assign
      frame f-1:row           = 2
      frame f-1:box           = true
      frame f-1:centered      = true
      frame f-button:centered = true
      frame f-button:row      = 20.

   assign
      frame a:centered = true
      frame a:row      = 10
      frame a:overlay  = true.

END PROCEDURE. /* ip-framesetup */

PROCEDURE ip-button:

   define input-output parameter perform-status as character
      format "x(25)" no-undo.

   if not batchrun and c-application-mode <> "API"
   then do:

      enable all with frame f-button.
      ststatus = stline[2].
      status input ststatus.
      on choose of b-update
      do:
      message "on choose of b-update" view-as alert-box.
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-update */

      on choose of b-add
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-add */

      on choose of b-delete
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-delete */

      on choose of b-option
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle
            l-title        = current-window:title
                           + " - "
                           + self:label.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-option */

      on choose of b-books
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle
            l-title        = current-window:title
                           + " - "
                           + self:label.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-books */

      on choose of b-detail
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle
            l-title        = current-window:title
                           + " - "
                           + self:label.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-detail */

      on choose of b-find
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle
            l-title        = current-window:title
                           + " - "
                           + self:label.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-find */

      on choose of b-end
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-1 no-pause.
         hide frame f-button no-pause.
      end. /* ON CHOOSE OF b-end */

      on cursor-up, f9 anywhere
      do:
         assign
            perform-status = "prev"
            l-focus        = focus:handle.
         apply "go" to frame f-button.
      end. /* ON CURSOR-UP, F9 ANYWHERE */

      on cursor-down, f10 anywhere
      do:
         assign
            perform-status = "next"
            l-focus        = focus:handle.
         apply "go" to frame f-button.
      end. /* ON CURSOR-DOWN, F10 ANYWHERE */

      on cursor-right anywhere
      do:
         l-focus = self:handle.
         apply "tab" to l-focus.
      end. /* ON CURSOR-RIGHT ANYWHERE */

      on cursor-left anywhere
      do:
         l-focus = self:handle.
         apply "ctrl-u" to l-focus.
      end. /* ON CURSOR-LEFT ANYWHERE */

      on f3 anywhere
         apply "choose" to b-add in frame f-button.
      on f5, ctrl-d anywhere
         apply "choose" to b-delete in frame f-button.

      on end-error anywhere
         apply "choose" to b-end in frame f-button.
      on window-close of current-window
         apply "choose" to b-end in frame f-button.

      on go anywhere
      do:
         if (lastkey    = keycode("cursor-down")
            or lastkey = keycode("cursor-up")
            or lastkey = keycode("f9")
            or lastkey = keycode("f10"))
         then
            return.
         else
            l-focus = focus:handle.
         apply "choose" to l-focus.
      end. /* ON GO ANYWHERE */

      wait-for
         go of frame f-button
         or window-close of current-window
         or choose of
         b-update,
         b-add,
         b-delete,
         b-option,
         b-books,
         b-detail,
         b-find,
         b-end
         focus
         l-focus.
      hide message no-pause.

   end. /* IF NOT BATCHRUN */
   else 
   if c-application-mode <> "API" then
      set perform-status.

END PROCEDURE. /* ip-button */

PROCEDURE valid-entity:

   define input  parameter p_faloc_entity like faloc_entity no-undo.
   define output parameter p_error        like mfc_logical  no-undo.

   /* CHECK ENTITY SECURITY */
   {us/gl/glenchk.i &entity=p_faloc_entity &entity_ok=valid_entity}

   /* IF LOCATION HAS ENTITY SECURITY THEN SET p_error TO YES */
   if not valid_entity
   then
      p_error = yes.
   else
      p_error = no.

END PROCEDURE. /* valid-entity */

/* STORE DATA FOR POSTING OF ACQUISITION COST AND ADJUSTMENTS */
PROCEDURE ip_posttogl:

   define variable l_entity_ok    like mfc_logical no-undo.
   define variable l_suspend      like mfc_logical no-undo.
   define variable l_uop          like mfc_logical no-undo.
   define variable l_post_yrper   like fabd_yrper no-undo.
   define variable l_daybook_desc like dy_desc no-undo.
   define variable l_error        as integer no-undo.
   define variable l_fa_id        like fa_id no-undo.
   define variable l_nozero_overrid like mfc_logical no-undo.
   if not available fa_mstr
   then
      return.
   l_fa_id = fa_id.
   /* NO POSTING BOOK SET UP */
   if l_post_bk = ""
   then
      return.
   /* ACCOUNT FOR DELETION OF MODIFIED ASSET */
   for each tt_pstast
      where tt_pstast.tt_asset = l_ast_del:
      delete tt_pstast.
   end. /* FOR EACH tt_pstast */
   if l_ast_del <> ""
   then do:
      l_ast_del = "".
      return.
   end.
   for first fab_det
      where fab_det.fab_domain = global_domain
      and   fab_fa_id   = l_fa_id
      and   fab_fabk_id = l_post_bk
   no-lock:
   end. /* FOR FIRST fab_det */
   /* ACCOUNT FOR DELETION OF MODIFIED POSTING BOOK */
   if not available fab_det
      and fa_dep
   then do:
      for each tt_pstast
         where tt_asset = l_fa_id:
         delete tt_pstast.
      end.

      return.

   end. /* IF NOT AVAILABLE fab_det */

   else do:
      /* DO NOT CONSIDER UNPOSTED ASSETS HAVING NON-ZERO */
      /* OVERRIDE ACCUMULATED DEPRECIATION AMOUNT        */
      if available fab_det
      then do:

         fab_post_oriden = fab_post_overridden.    

         if fab_ovramt         <> 0 
            and (fab_cst_adjper =  "" and fab_post_oriden = no)   
         then do:
		    /* also need to check other books. */
			l_nozero_overrid = no.
			for each fab_det
				where fab_det.fab_domain = global_domain
				and   fab_fa_id = l_fa_id
				and   fab_fabk_id <> l_post_bk
				and   can-find(first fabk_mstr
								  where fabk_domain = global_domain
								  and   fabk_id     = fab_fabk_id
								  and   fabk_post   = yes):
				
				fab_post_oriden = fab_post_overridden. 
				if (fab_ovramt         = 0 or fab_ovramt = ? ) 
					or (fab_cst_adjper   <> "" or fab_post_oriden = yes) 
			    then l_nozero_overrid = yes.
		    end.
			if(l_nozero_overrid = no) then return.
	     end.
      end. /* IF AVAILABLE fab_det */

      /* If user press F4 or End button and  the Acuisition is not posted */
      for first fact_ctrl where fact_domain = global_domain no-lock:

         if not fact_post_in_fa_maint then
            return.

         l_post_to_gl = fact_post_to_gl.
      end.

      if c-application-mode = "API" then l_post_to_gl = yes.

      if (fa_acq_post =no and (perform-status = "end" or lastkey = keycode("F4")) and fa_post = no)
      then do:
      message "2:EnD" view-as alert-box.
         for first fab_det no-lock
            where fab_fa_id = l_fa_id
            and fab_fabk_id = l_post_bk
            and fab_domain = global_domain:
         end.
         if available(fab_det) then
            l_cost = fab_amt.
         else
            l_cost = fa_puramt.
      end.
      if l_cost = 0 then
         l_cost = fa_puramt - fa_posted_puramt.
      if l_cost = 0
         and (fa_acq_post =no 
              and ( perform-status = "end" 
                    or lastkey = keycode("F4")))
      then  /* LOOK INTO SECONDARY POSTING BOOKS FOR ANY COST, ASSET WILL BE POSTED IN THIS CASE AS PPB COST = 0*/  
         for each fab_det
            where fab_det.fab_domain = global_domain
            and   fab_fa_id = l_fa_id
            and   fab_fabk_id <> l_post_bk
            and   can-find(first fabk_mstr
                              where fabk_domain = global_domain
                              and   fabk_id     = fab_fabk_id
                              and   fabk_post   = yes)
         no-lock:

            fab_post_oriden = fab_det.fab_post_overridden.    

            if fab_ovramt <> 0
            then do:
               if fab_cst_adjper <> ""
               then
                  l_cost = l_cost + fab_det.fab_amt.
               if fab_cst_adjper = "" and fab_post_oriden = yes       
               then
                  l_cost = l_cost + fab_det.fab_amt.   
            end.
            else
               l_cost = l_cost + fab_det.fab_amt.

            if l_cost <> 0 
            then 
               leave.
         end.
      /* Create new(view old FA) FA and Press F4 */
      /* Changed the cost on header for a posted FA*/
      if (l_astpost and l_cost <> 0)
         or ((perform-status = "end" or lastkey = keycode("F4")) and l_cost <> 0 and fa_acq_post = no )
         or  (l_cost <> 0 and fa_puramt <> fa_posted_puramt and fa_acq_post = yes and fa_post = no
         and (perform-status = "end" or lastkey = keycode("F4")))
      then do:
         
         fab_post_oriden = no.
         for first fab_det 
            where fab_fa_id = l_fa_id
            and fab_fabk_id = l_post_bk
            and fab_domain = global_domain
          no-lock:
            fab_post_oriden = fab_det.fab_post_overridden.    

            if fab_ovramt = 0 then fab_post_oriden = no.
         end.         

         if fab_post_oriden = yes
         then  
            for first fabd_det
               where fabd_det.fabd_domain = global_domain
               and ( fabd_entity  = fa_entity
               and   fabd_fa_id   = l_fa_id
               and   fabd_fabk_id = l_post_bk
               and   fabd_suspend = no
               and  (fabd_retired = no or fabd_rt_period = yes)
               )
               use-index fabd_fa_id
            no-lock:
            end. /* FOR FIRST fabd_det */
         else  
            for first fabd_det
               where fabd_det.fabd_domain = global_domain
               and ( fabd_entity  = fa_entity
               and   fabd_fa_id   = l_fa_id
               and   fabd_fabk_id = l_post_bk
               and   fabd_post    = no
               and   fabd_suspend = no
               and  (fabd_retired = no or fabd_rt_period = yes)
               )
               use-index fabd_fa_id
            no-lock:
            end. /* FOR FIRST fabd_det */


         if available fabd_det
         then
            assign
               l_uop        = no
               l_post_yrper = fabd_yrper.

         else do:

            if fa_dep
            then do:
               /* ACQUISITION LOGIC FOR UN-POSTED UOP TYPE OF ASSETS */
               if can-find(first famt_mstr
                  where famt_mstr.famt_domain = global_domain
                  and   famt_id   = fab_famt_id
                  and   famt_type = "2")
               then do:
                  l_uop = yes.
                  {us/gp/gprunp.i "fapl" "p" "fa-get-per"
                     "(input  fab_date,
                       input  '',
                       output l_post_yrper,
                       output l_error)"}
                  if l_error <> 0
                  then
                     return.
               end. /* IF CAN-FIND(FIRST famt_mstr */
               else
                  for first fabd_det
                     where fabd_det.fabd_domain = global_domain
                     and ( fabd_entity  = fa_entity
                     and   fabd_fa_id   = l_fa_id
                     and   fabd_fabk_id <> l_post_bk
                     and   fabd_post    = no
                     and   fabd_suspend = no
                     and  (fabd_retired = no 
                           or fabd_rt_period = yes) )
                     use-index fabd_fa_id
                     no-lock:
                  end. /* FOR FIRST fabd_det */
                  if available fabd_det
                  then
                     assign
                        l_uop        = no
                        l_post_yrper = fabd_yrper.

            end. /* IF fa_dep */
            /* ACQUISITION LOGIC FOR NON-DEPRECIATING ASSETS */
            else do:

               l_uop = no.
               {us/gp/gprunp.i "fapl" "p" "fa-get-per"
                  "(input  fa_startdt,
                    input  '',
                    output l_post_yrper,
                    output l_error)"}

               if l_error <> 0
               then
                  return.

            end. /* ELSE DO */

         end. /* ELSE DO */

         {us/gp/gprunp.i "fapl" "p" "fa-get-suspend"
            "(input  l_fa_id,
              input  l_post_bk,
              input  l_post_yrper,
              output l_suspend)"}

         if l_suspend
         then
            return.

         /* DEFAULT FA DAYBOOK; DOC TYPE AND TRANSACTION TYPE - "FA"*/
         {us/bbi/gprun.i ""gldydft.p""
            "(input  ""FA"",
              input  ""FA"",
              input  fa_entity,
              output dft-daybook,
              output l_daybook_desc)"}

         assign
            l_postgl  = no
            l_effdate = today.

         postloop:
         repeat on error undo, retry:
            if fa_dep
            then do:
               l_cost_acq = 0.
               for each fab_det
                  no-lock
                     where fab_det.fab_domain = global_domain
                     and   fab_fa_id = l_fa_id
                     and   can-find(first fabk_mstr
                     where fabk_domain = global_domain
                     and   fabk_id     = fab_fabk_id
                     and   fabk_post   = yes):

                  for last faba_det no-lock
                     where faba_domain = fab_domain
                     and   faba_fa_id   = fab_fa_id
                     and   faba_fabk_id = fab_fabk_id
                  use-index faba_fa_id:
                     l-acct-seq = faba_glseq.
                  end.  /* FOR LAST faba_det */

                  for each faba_det
                     no-lock
                        where faba_domain  = fab_domain
                        and   faba_fa_id   = fab_fa_id
                        and   faba_fabk_id = fab_fabk_id
                        and   faba_glseq   = l-acct-seq:

                     /* INITIALIZE SETTINGS */
                     {us/gp/gprunp.i "gpglvpl" "p" "initialize"}
                     /* SET ENTITY VERIFICATION TO YES */
                     {us/gp/gprunp.i "gpglvpl" "p" "set_coa_restriction_ver" "(input true, input fa_entity)"}
                     /* ACCT/SUB/CC/PROJ VALIDATION */
                     {us/gp/gprunp.i "gpglvpl" "p" "validate_fullcode"
                        "(input faba_acct,
                          input faba_sub,
                          input faba_cc,
                          input faba_proj,
                          input (""faba_acct"" + ""_"" + faba_det.faba_acctype),
                          output valid_acct)"}

                     if valid_acct = no
                     then
                        return.
                  end. /* FOR EACH faba_det */

                  fab_post_oriden = fab_det.fab_post_overridden.    

                  if fab_ovramt <> 0
                  then do:
                     if fab_cst_adjper <> ""
                     then
                        l_cost_acq = l_cost_acq + fab_det.fab_amt.
                     if fab_cst_adjper = "" and fab_post_oriden = yes       
                     then
                        l_cost_acq = l_cost_acq + fab_det.fab_amt.  
                  end.
                  else
                     l_cost_acq = l_cost_acq + fab_det.fab_amt.
               end. /* FOR EACH fab_det .... */

               if l_cost_acq = 0         /* THERE IS NOT COST ON ANY BOOK DO NOT POST ACQUISTION FOR THE ASSET */
                  and l_cost  = 0
               then 
                  return.
               l_postgl = l_post_to_gl.
               if c-application-mode <> "API" then
                  display
                     l_postgl
                  with frame a.

               if c-application-mode <> "API" then do:
                  prompt-for
                     l_postgl
                     when (fa_acq_post = no)
                  with frame a.

                  l_postgl = input l_postgl.

                  if fa_acq_post = yes then
                     l_postgl  = yes.
               end.
               if not l_postgl
               then do:
                  if ( (perform-status = "end" or lastkey = keycode("F4")) and
                     (fa_acq_post = no or fa_puramt <> fa_posted_puramt)) and
                     c-application-mode <> "API"
                  then do:
                     if l_postgl = no 
                     then do:
                        /* Leave without posting */
                        {us/bbi/pxmsg.i &MSGNUM=11151 &ERRORLEVEL=1 &CONFIRM=l-quit &CONFIRM-TYPE='LOGICAL'}
                        if not l-quit then
                           undo postloop, retry postloop.
                     end.
                  end.
                  for each tt_pstast
                     where tt_pstast.tt_asset = l_fa_id:
                     delete tt_pstast.
                  end. /* FOR EACH tt_pstast */

                  assign
                     l_cost    = 0
                     l_astpost = no.

                  hide frame a.
                  leave postloop.
               end. /* IF NOT l_postgl */

               postloop1:
               do on error undo, retry:
                  /* ACCEPT BELOW FIELDS ONLY IF USER WANTS TO POST */
                  /* THE ASSETS TO GL                               */
                  if fa_post
                  then
                     assign
                        l_effdate = today.
                  else
                     assign
                        l_effdate = fa_acquis_effectivedate.
 
                  if c-application-mode <> "API" 
                  then do:
                     display
                        l_effdate
                     with frame a.

                     prompt-for l_effdate
                        when (fa_acq_post = yes)
                     with frame a.

                     l_effdate = input l_effdate.
                  end.
                  /* VALIDATION FOR l_effdate */
                  if l_effdate = ?
                  then do:
                     /* DATE REQUIRED */
                     {us/bbi/pxmsg.i &MSGNUM=711 &ERRORLEVEL=3}

                     if not batchrun and c-application-mode <> "API"
                     then do:
                        next-prompt
                           l_effdate
                        with frame a.
                        undo postloop1, retry postloop1.
                     end. /* IF NOT batchrun */
                     else
                        return.
                  end. /* IF l_effdate = ? */
                  else do:
                     /* VALIDATE EFFECTIVE DATE AGAINST GL CALENDAR */
                     for first en_mstr
                        where en_mstr.en_domain = global_domain
                        and   en_entity = fa_entity
                     no-lock:

                     {us/bbi/gprun.i ""gpglef1.p""
                        "(input ""FA"",
                          input en_entity,
                          input l_effdate,
                          output gpglef_result,
                          output gpglef_msg_nbr)"}

                     if gpglef_result > 0
                     then do:
                        /* INVALID/CLOSED PERIOD OR INACTIVE ENTITY */
                        {us/bbi/pxmsg.i &MSGNUM=gpglef_msg_nbr &ERRORLEVEL=3
                           &MSGARG1=en_entity}

                         if not fa_acq_post
                            and not batchrun 
                            and c-application-mode <> "API"
                         then 
                            undo postloop, retry postloop.

                        if not batchrun and c-application-mode <> "API"
                        then do:
                           next-prompt
                              l_effdate
                           with frame a.
                           undo postloop1, retry postloop1.
                           end. /* IF NOT batchrun */
                           else do:
                              return.
                           end.
                        end. /* IF gpglef_result > 0 */
                     end. /* FOR FIRST en_mstr */
                  end. /* ELSE DO */
                  /* END VALIDATION FOR l-effdate */
               end. /* DO ON ERROR UNDO, RETRY */
               if l_cost <> 0 
               then do:
                  find first tt_pstast
                     where tt_asset = l_fa_id
                  exclusive-lock no-error.
                  if not available tt_pstast
                     and can-find(fa_mstr
                     where fa_mstr.fa_domain    = global_domain
                     and   fa_mstr.fa_id        = l_fa_id
                     and   fa_mstr.fa_acq_post  = no)
                  then do:
                     /* CREATE tt_pstast RECORDS FOR POSTING BOOKS
                        WHEN POSTING ACQUISITION FOR FIRST TIME */

                     assign
                        l_cost        = 0
                        l_primbk_cost = 0.

                     for each fab_det
                        where fab_det.fab_domain   = global_domain
                        and   fab_det.fab_fa_id    = l_fa_id
                        and   fab_det.fab_fabk_id  = l_post_bk no-lock
                     break by fab_det.fab_fabk_id:
                        
                        assign
                            fab_post_oriden = fab_det.fab_post_overridden
                            l_primary_override = fab_det.fab_post_overridden.       

                        if fab_ovramt <> 0
                        then do:
                           if fab_cst_adjper <> ""
                           then
                              l_cost = l_cost + fab_det.fab_amt.
                           if fab_cst_adjper = "" and fab_post_oriden = yes       
                           then
                              l_cost = l_cost + fab_det.fab_amt.    
                        end.
                        else
                           l_cost = l_cost + fab_det.fab_amt.
                        if last-of(fab_det.fab_fabk_id)
                           and l_cost <> 0
                        then do:                                            
                           create tt_pstast.
                           assign
                              tt_asset      = l_fa_id
                              tt_book       = fab_det.fab_fabk_id
                              tt_daybk      = dft-daybook
                              tt_effdt      = l_effdate
                              tt_cost       = l_cost
                              tt_entity     = fa_entity
                              tt_pstyrper   = l_post_yrper
                              tt_uop        = l_uop
                              l_astpost     = no
                              l_primbk_cost = l_cost.
                        end. /* IF LAST-OF(fab_det.fab_fabk_id) AND l_cost <> 0 */
                     end. /* FOR EACH fab_det */

                     l_cost = 0.
                     for each fab_det
                           where fab_det.fab_domain = global_domain
                           and fab_det.fab_fa_id    = l_fa_id
                           and fab_det.fab_fabk_id  <> l_post_bk no-lock
                           break by fab_det.fab_fabk_id:
                        if first-of(fab_det.fab_fabk_id)
                           and can-find(first fabk_mstr
                           where fabk_mstr.fabk_domain = global_domain
                           and fabk_mstr.fabk_id       = fab_det.fab_fabk_id
                           and fabk_mstr.fabk_post     = yes)
                        then do:
                        
                           for each fabk_mstr where fabk_mstr.fabk_domain = global_domain
                                and fabk_mstr.fabk_id       = fab_det.fab_fabk_id 
                           no-lock:
                              assign
                                 l_fabk_post = fabk_mstr.fabk_post
                                 l_delta     = fabk_mstr.fabk_delta.
                           end.

                           for each bffab_det
                              where bffab_det.fab_domain  = global_domain
                              and   bffab_det.fab_fa_id   = l_fa_id
                              and   bffab_det.fab_fabk_id = fab_det.fab_fabk_id
                           no-lock:

                              fab_post_oriden = bffab_det.fab_post_overridden.    

                              l-is-delta-zero = no.  

                              if bffab_det.fab_ovramt <> 0 and fab_post_oriden = yes then l-is-delta-zero = yes. 
                              if l_delta = yes and fab_post_oriden = no and bffab_det.fab_ovramt = 0 and l_primary_override = yes then l-is-delta-zero = yes.

                              if bffab_det.fab_ovramt <> 0
                              then do:
                                 if bffab_det.fab_cst_adjper <> ""
                                 then
                                    l_cost = l_cost + bffab_det.fab_amt.
                                 if bffab_det.fab_cst_adjper = "" and fab_post_oriden = yes    
                                 then
                                    l_cost = l_cost + bffab_det.fab_amt.    
                              end.
                              else
                                 l_cost = l_cost + bffab_det.fab_amt.
                           end.

                           if l_delta
                              and l_fabk_post
                              and (fab_det.fab_ovramt = 0 or fab_post_oriden = yes)       
                           then do:
                              create tt_pstast.
                              assign
                                 tt_asset    = l_fa_id
                                 tt_book     = fab_det.fab_fabk_id
                                 tt_daybk    = dft-daybook
                                 tt_effdt    = l_effdate
                                 tt_cost     = l_cost - l_primbk_cost
                                 tt_entity   = fa_entity
                                 tt_pstyrper = l_post_yrper
                                 tt_uop      = l_uop
                                 l_astpost   = no
                                 l_cost      = 0.
                              if l-is-delta-zero = yes and tt_cost = 0 then tt_cost = 0.0001. /* After reading the code, this 0.0001 logic is try to always do the posings even tt_cost is zero */
                           end. /* IF l_cost <> 0 */
                           else do:
                              if l_cost <> 0 and l_fabk_post
                              then do:
                                 create tt_pstast.
                                 assign
                                    tt_asset    = l_fa_id
                                    tt_book     = fab_det.fab_fabk_id
                                    tt_daybk    = dft-daybook
                                    tt_effdt    = l_effdate
                                    tt_cost     = l_cost
                                    tt_entity   = fa_entity
                                    tt_pstyrper = l_post_yrper
                                    tt_uop      = l_uop
                                    l_astpost   = no
                                    l_cost      = 0.
                              end. /* if l_cost <> 0 */
                           end. /* ELSE DO */
                        end.
                     end. /* FOR EACH fab_det */
                     l_primbk_cost = 0.
                     l_delta       = no.
                  end. /* IF NOT AVAILABLE tt_pstast */
                  else 
                  if not available tt_pstast
                     and can-find(fa_mstr
                     where fa_mstr.fa_domain    = global_domain
                     and   fa_mstr.fa_id        = l_fa_id
                     and   fa_mstr.fa_acq_post  = yes
                     and   fa_mstr.fa_post      = no)
                  then do:
                     if not l-delete-it
                     then do:
                        create tt_pstast.
                        assign
                           tt_asset      = l_fa_id
                           tt_book       = l_primary_book
                           tt_daybk      = dft-daybook
                           tt_effdt      = l_effdate
                           tt_cost       = l_cost
                           tt_entity     = fa_entity
                           tt_pstyrper   = l_post_yrper
                           tt_uop        = l_uop
                           l_astpost     = no.
                     end.  /* IF NOT l-delete-it */
                     else do:
                        for each fab_det
                           where fab_det.fab_domain   = global_domain
                           and   fab_det.fab_fa_id    = l_fa_id
                           and   fab_det.fab_fabk_id  = l_post_bk 
                           and   fab_det.fab_resrv    = 0 no-lock
                        break by fab_det.fab_fabk_id:
                           create tt_pstast.
                           assign
                              tt_asset      = l_fa_id
                              tt_book       = l_post_bk
                              tt_daybk      = dft-daybook
                              tt_effdt      = l_effdate
                              tt_cost       = l_cost
                              tt_entity     = fa_entity
                              tt_pstyrper   = l_post_yrper
                              tt_uop        = l_uop
                              l_astpost     = no
                              l_primbk_cost = l_cost
                              tt_cost       = -1 * tt_cost.
                        end. /* FOR EACH fab_det */

                        for each fab_det
                           where fab_det.fab_domain = global_domain
                           and fab_det.fab_fa_id    = l_fa_id
                           and fab_det.fab_fabk_id  <> l_post_bk 
                        no-lock break by fab_det.fab_fabk_id:
                           if first-of(fab_det.fab_fabk_id)
                              and can-find(first fabk_mstr
                              where fabk_mstr.fabk_domain = global_domain
                              and fabk_mstr.fabk_id       = fab_det.fab_fabk_id
                              and fabk_mstr.fabk_post     = yes)
                           then do:
                              {us/gp/gprunp.i "fapl" "p" "fa-get-cost"
                                "(input  fab_det.fab_fa_id,
                                  input  fab_det.fab_fabk_id,
                                  output l_cost)"}
                              if l_cost <> 0
                                 and fab_det.fab_ovramt = 0
                              then do:
                                 create tt_pstast.
                                 assign
                                    tt_asset      = l_fa_id
                                    tt_book       = fab_det.fab_fabk_id
                                    tt_daybk      = dft-daybook
                                    tt_effdt      = l_effdate
                                    tt_cost       = l_cost 
                                    tt_entity     = fa_entity
                                    tt_pstyrper   = l_post_yrper
                                    tt_uop        = l_uop
                                    l_astpost     = no.
    
                                 if can-find(first fabk_mstr
                                    where fabk_mstr.fabk_domain = global_domain
                                    and fabk_mstr.fabk_id       = fab_det.fab_fabk_id
                                    and fabk_mstr.fabk_post     = yes
                                    and fabk_mstr.fabk_delta    = yes)
                                 then
                                    tt_cost = l_cost - l_primbk_cost.  

                                 if l-delete-it 
                                 then 
                                    tt_cost = -1 * tt_cost.
                              end. /* IF l_cost <> 0 */
                           end. /* IF FIRST-OF(fab_det ... */ 
                        end. /* FOR EACH fab_det */
                        l_primbk_cost = 0.
                     end. /* ELSE DO: IF NOT l-delete-it */
                  end. /* else if not available tt_pstast */
                  else do:
                     for each tt_pstast
                        where tt_pstast.tt_asset = l_fa_id
                     no-lock:
                        assign
                           tt_daybk    = dft-daybook
                           tt_entity   = fa_entity
                           tt_pstyrper = l_post_yrper
                           tt_effdt    = l_effdate
                           tt_uop      = l_uop
                           l_astpost   = no
                           l_cost      = 0.
                     end. /* FOR EACH tt_pstast */
                     pause.
                  end. /* ELSE DO */
               end. /* IF l_cost <> 0 THEN DO*/
            end. /* IF fa_dep */

            if c-application-mode <> "API" then
               hide frame a.
            leave postloop.
         end. /* postloop */

      end. /* IF l_astpost */
      else do:
         if l_cost = 0
            or can-find (first tt_pstast
            where tt_pstast.tt_asset = l_fa_id
            no-lock)
         then do:
            for each tt_pstast
               where tt_pstast.tt_asset = l_fa_id:
               delete tt_pstast.
            end.
         end. /* IF l_cost = 0 */
      end. /* ELSE DO */

   end. /* ELSE DO */

END PROCEDURE. /* ip_posttogl */

PROCEDURE ip-headercost:

   define parameter buffer fa_mstr for fa_mstr.
   define parameter buffer fab_det for fab_det.
   define input  parameter p_puramt_entered like mfc_logical no-undo.
   define input  parameter p_salvamt_entered like mfc_logical no-undo.
   define input  parameter p_cost           like fa_puramt   no-undo.
   define input  parameter p_fa_id          like fa_id       no-undo.
   define input  parameter p_fabk_id        like fabk_id       no-undo.
   define input  parameter p_fab_resrv      like fab_resrv       no-undo.
   define input  parameter p_post_bk        like fabk_id       no-undo.
   define input  parameter p_delta          like mfc_logical no-undo.
   define output parameter p_perform_status as   character format "x(25)" no-undo.
   define output parameter p_err_nbr        as   integer   format "9999" no-undo.
   define input-output parameter  p_primbk_cost   like fa_puramt   no-undo.

   if (p_puramt_entered
      and p_cost <> 0)
      or  p_salvamt_entered
   then do:
      if can-find(fa_mstr
         where fa_mstr.fa_domain    = global_domain
         and   fa_mstr.fa_id        = p_fa_id
         and   fa_mstr.fa_acq_post  = yes)
         and can-find(first fabk_mstr
         where fabk_mstr.fabk_domain = global_domain
         and   fabk_mstr.fabk_id     = p_fabk_id
         and   fabk_mstr.fabk_post   = yes)
         and   p_puramt_entered
      then do:
         find first tt_pstast
            where tt_asset = p_fa_id
            and   tt_book  = p_fabk_id
            no-error.
         if not available tt_pstast
         then do:
            create tt_pstast.
            assign
               tt_pstast.tt_asset = p_fa_id
               tt_pstast.tt_book  = p_fabk_id
               tt_pstast.tt_effdt = today.

            if  p_delta   = yes
               and p_fabk_id <> p_post_bk
            then do:
               tt_pstast.tt_cost     = p_cost - p_primbk_cost.
            end.
            else do:
               assign
                  tt_pstast.tt_cost = p_cost
                  p_primbk_cost     = p_cost.
            end. /* else do */
         end. /* IF NOT AVAILABLE tt_pstast */
         else do:
            if  p_delta   = yes
               and p_fabk_id <> p_post_bk
            then do:
               assign
                  tt_pstast.tt_effdt = today
                  tt_pstast.tt_cost  = tt_pstast.tt_cost + (p_cost - p_primbk_cost).
            end. /* IF p_delta = YES */
            else do:
               assign
                  tt_pstast.tt_effdt = today
                  tt_pstast.tt_cost  = tt_pstast.tt_cost + p_cost
                  p_primbk_cost      = p_cost.
            end. /* ELSE DO */
         end. /* ELSE DO */
      end. /* IF CAN-FIND(fa_mstr) */

      /* ADDED 1ST INPUT PARAMETER AS yes THIS WILL CHECK THAT THE     */
      /* NEXT CALENDAR IS DEFINED FOR AN ASSET PLACED IN A SHORT YEAR. */

      /* ADDED 5TH AND 6TH PARAMETERS AS THE YEAR FOR ADJUSTMENT */
      /* AND DEPRECIATION CALCULATION FOR FOLLOWING YEARS IS     */
      /* NEEDED OR NOT RESPECTIVELY. THESE PARAMETERS WILL BE    */
      /* zero AND no RESPECTIVELY EXCEPT FOR UTILITY utrgendp.p  */
      {us/bbi/gprun.i ""fadpbla.p""
         "(input  yes,
           input  p_fab_resrv,
           buffer fa_mstr,
           buffer fab_det,
           input  0,
           input  no,
           output p_err_nbr)"}

      if p_err_nbr <> 0
      then do:
         {us/bbi/pxmsg.i &MSGNUM=p_err_nbr &ERRORLEVEL=3}
         p_perform_status = "undo".
         return.
      end. /* IF l-err-nbr <> 0 */
   end. /* IF (fa_puramt ENTERED */

END PROCEDURE. /* ip-headercost */

PROCEDURE validate-api-data:
   define output parameter l_error        like mfc_logical  no-undo.

   define variable l_faCnt as integer no-undo.
   define variable l_bookCnt1 as integer no-undo.
   define variable l_bookCnt2 as integer no-undo.
   define variable l_IsBookError as logical no-undo.
   define variable BufErrorMessage as character no-undo.
   define variable ErrorMessage as character no-undo.
   define buffer bu_fa_mstr for fa_mstr.
   define variable MessageArg as character no-undo.
   define variable MessageArg2 as character no-undo.

   l_error = no.

   /*  1    Have one and only one record exists in ttFixedAssetMaster table.
   Err Msg: You must provide one record only in ttFixedAssetMaster table */
   l_faCnt = 0.
   for each ttFixedAssetMaster:
      l_faCnt = l_faCnt + 1.
   end.
   if l_faCnt <> 1
   then do:
      MessageArg = "ttFixedAssetMaster".
      {us/bbi/pxmsg.i &MSGNUM=16101 &ERRORLEVEL=3 &MSGARG1=MessageArg}
      l_error = yes.
   end.

   /* 2   Make sure the Fixed Asset, Class and Location exists in the database.
   There is a default system validation to cover this, no error message needed. */
   /* set default class and location if empty */
   for first ttFixedAssetMaster where ttFixedAssetMaster.operation = {&MODIFY}:

      if ttFixedAssetMaster.faFaclsId = ""
      then do:
         ttFixedAssetMaster.faFaclsId = ?.
      end.

      if ttFixedAssetMaster.faFalocId = ""
      then do:
         ttFixedAssetMaster.faFalocId = ?.
      end.
   end.

   /* 3   Block the API if Fixed Asset has done a depreciation post.
   Err Msg: You cannot modify a depreciated fixed asset */
   find first ttFixedAssetMaster no-lock no-error.
   if available ttFixedAssetMaster
   then do:
      find first fa_mstr where fa_domain = global_domain and
         fa_id = ttFixedAssetMaster.faId and
         fa_post = yes
      no-lock no-error.
      if available fa_mstr
      then do:
         {us/bbi/pxmsg.i &MSGNUM=16102 &ERRORLEVEL=3}
         l_error = yes.
      end.
   end.

   /* 4   It's not allowed to use ttFixedAssetBookAdj table currently.
   Err Msg: Adjustments using table ttFixedAssetBookAdj are not implemented */
   find first ttFixedAssetBookAdj no-lock no-error.
   if available ttFixedAssetBookAdj
   then do:
      MessageArg = "ttFixedAssetBookAdj".
      {us/bbi/pxmsg.i &MSGNUM=16104 &ERRORLEVEL=3 &MSGARG1= MessageArg}
      l_error = yes.
   end.

   /* 5   The value of Operation field must be "A" or "M"
   Err Msg: You must specify a value of A or M in the field Operation */
   for each ttFixedAssetMaster where ttFixedAssetMaster.operation <> {&ADD} and
         ttFixedAssetMaster.operation <> {&MODIFY}
   no-lock:
      MessageArg = "Operation".
      {us/bbi/pxmsg.i &MSGNUM=16105 &ERRORLEVEL=3 &MSGARG1= MessageArg}
      l_error = yes.
   end.
   for each ttFixedAssetBooks where ttFixedAssetBooks.operation <> {&ADD} and
         ttFixedAssetBooks.operation <> {&MODIFY}
   no-lock:
      MessageArg = "Operation".
      {us/bbi/pxmsg.i &MSGNUM=16105 &ERRORLEVEL=3 &MSGARG1=MessageArg}
      l_error = yes.
   end.

   /* 6   If records exists in ttFixedAssetBooks table, then the field value of ttFixedAssetMaster.fabutton can only be "Books"
   Err Msg: The field ttFixedAssetMaster must contain "Books" */
   find first ttFixedAssetBooks no-lock no-error.
   if available ttFixedAssetBooks
   then do:
      for each ttFixedAssetMaster where ttFixedAssetMaster.fabutton <> "Books"
      no-lock:
         MessageArg = "ttFixedAssetMaster.fabutton".
         MessageArg2 = "Books".
         {us/bbi/pxmsg.i &MSGNUM=16106 &ERRORLEVEL=3 &MSGARG1=MessageArg &MSGARG2=MessageArg2}
         l_error = yes.
      end.
   end.

   /* 7   The value in ttFixedAssetBooks.operation should be consistent with ttFixedAssetMaster.operation */
   /* Err Msg: The values of the field "Operation" must be same in both tables. */
   /*          Please check "ttFixedAssetBooks" and "ttFixedAssetMaster" */
   find first ttFixedAssetMaster no-lock no-error.
   if available ttFixedAssetMaster
   then do:
      for each ttFixedAssetBooks where ttFixedAssetBooks.operation <> ttFixedAssetMaster.operation
      no-lock:
         MessageArg = "Operation".
         {us/bbi/pxmsg.i &MSGNUM=16108 &MSGBUFFER = BufErrorMessage &ERRORLEVEL=1 &MSGARG1=MessageArg}
         ErrorMessage = BufErrorMessage.
         MessageArg = "ttFixedAssetBooks".
         MessageArg2 = "ttFixedAssetMaster".
         {us/bbi/pxmsg.i &MSGNUM=16109 &MSGBUFFER = BufErrorMessage &ERRORLEVEL=1 &MSGARG1=MessageArg &MSGARG2=MessageArg2}
         ErrorMessage = ErrorMessage + ". " + BufErrorMessage.
         {us/bbi/pxmsg.i &MSGTEXT=ErrorMessage &ERRORLEVEL=3}
         l_error = yes.
      end.
   end.

   /* 8   When try to add a Fixed Asset using API, then the books records in ttFixedAssetBooks table should */
   /*     be consistent with the books in the FA Class.  */
   /*  Err Msg: You must provide all the books of the specified class */
   l_IsBookError = no.
   find first ttFixedAssetMaster no-lock no-error.
   find first ttFixedAssetBooks no-lock no-error.
   if available ttFixedAssetMaster and ttFixedAssetMaster.faFaclsId <> ? and ttFixedAssetMaster.faFaclsId <> ''
      and available ttFixedAssetBooks
   then do:
      /* validate the books */
      if ttFixedAssetMaster.operation = {&ADD}
      then do:
         l_bookCnt1 = 0.
         for each fadf_mstr where fadf_domain = global_domain and
               fadf_facls_id = ttFixedAssetMaster.faFaclsId 
         no-lock:
            l_bookCnt1 = l_bookCnt1 + 1.

            /* make sure books are all exists in the ttFixedAssetBooks */
            find first ttFixedAssetBooks where ttFixedAssetBooks.faId = ttFixedAssetMaster.faId and
               ttFixedAssetBooks.fabFabkId = fadf_mstr.fadf_fabk_id no-lock no-error.
            if not available ttFixedAssetBooks
            then do:
               l_IsBookError = yes.
            end.
         end.

         /* validate books count */
         l_bookCnt2 = 0.
         for each ttFixedAssetBooks where ttFixedAssetBooks.faId = ttFixedAssetMaster.faId:
            l_bookCnt2 = l_bookCnt2 + 1.
         end.

         if l_bookCnt1 <> l_bookCnt2
         then do:
            l_IsBookError = yes.
         end.
      end. /* if ttFixedAssetMaster.operation = {&ADD} */
   end.
   if l_IsBookError = yes
   then do:
      {us/bbi/pxmsg.i &MSGNUM=16110 &ERRORLEVEL=3}
      l_error = yes.
   end.

   /* 9   When try to modify a Fixed Asset using API, then the books records in ttFixedAssetBooks */
   /*     table should be exists in the database. */
   /*  Err Msg: The book(s) <xxx> doesn't exist in the database. */
   /* System can cover this validation */

   /* 10  When try to modify a Fixed Asset using API and no records in the ttFixedAssetBooks table,  */
   /*     then this Fixed Asset should only has one book with base currency and not do the acquisition post.  */
   /*  Err Msg: You cannot modify a fixed asset if it has more than one book. */
   /*  You cannot modify a fixed asset if the book curr is not base curr. */
   /*  You cannot modify a fixed asset with acquisition posted  */
   find first ttFixedAssetMaster no-lock no-error.
   find first ttFixedAssetBooks no-lock no-error.
   if available ttFixedAssetMaster and ttFixedAssetMaster.operation = {&MODIFY} and not available ttFixedAssetBooks 
   then do:
      l-count = 0.
      find first fa_mstr where fa_mstr.fa_domain = global_domain
         and fa_mstr.fa_id = ttFixedAssetMaster.faId no-lock no-error.
      if available fa_mstr 
      then do:
         for each fab_det where fab_det.fab_fa_id = fa_mstr.fa_id
               and fab_det.fab_domain = global_domain 
         no-lock:
            for first fabk_mstr where fabk_mstr.fabk_domain = global_domain
               and fabk_mstr.fabk_id     = fab_fabk_id 
            no-lock:
               find first cu_mstr where cu_mstr.oid_cu_mstr = fabk.oid_cu_mstr no-lock no-error.
               if available cu_mstr then
                  l-book-currency = cu_mstr.cu_curr.
            end.
            l-count =  l-count + 1.
         end. /* for each fab_det */

         if l-count > 1
         then do:
            {us/bbi/pxmsg.i &MSGNUM=16112 &ERRORLEVEL=3}
            l_error = yes.
         end.
         if l-book-currency <> base_curr
         then do:
            {us/bbi/pxmsg.i &MSGNUM=16113 &ERRORLEVEL=3}
            l_error = yes.
         end.
         if fa_acq_post = yes
         then do:
            {us/bbi/pxmsg.i &MSGNUM=16114 &ERRORLEVEL=3}
            l_error = yes.
         end.
      end. /* if available fa_mstr */
   end. /* if available ttFixedAssetMaster */

   /* 11  The field value in ttFixedAssetMaster.faId should be the same with field value in ttFixedAssetBooks.faId. */
   /*   Err Msg: The value in field faId <faId> is inconsistent between ttFixedAssetMaster and ttFixedAssetBooks. */
   /* Not needed this validation */

   /* 12  faPuramt is mandatory. When add a Fixed Asset(Feature 1 & 2) or */
   /*   Only modify the header cost (Feature 4) this field is mandatory.  */
   /*   Err Msg: You must provide the value in the field faPuramt */
   find first ttFixedAssetMaster no-lock no-error.
   if ttFixedAssetMaster.operation = {&ADD} and
      (ttFixedAssetMaster.faPuramt = 0 or ttFixedAssetMaster.faPuramt = ?)
   then do:
      MessageArg = "faPuramt".
      {us/bbi/pxmsg.i &MSGNUM=16118 &ERRORLEVEL=3 &MSGARG1=MessageArg}
      l_error = yes.
   end.
   if ttFixedAssetMaster.operation = {&MODIFY} and not can-find(first ttFixedAssetBooks)
      and (ttFixedAssetMaster.faPuramt =0 or ttFixedAssetMaster.faPuramt = ?)
   then do:
      MessageArg = "faPuramt".
      {us/bbi/pxmsg.i &MSGNUM=16118 &ERRORLEVEL=3 &MSGARG1=MessageArg}
      l_error = yes.
   end.

   /* 13  bookCost: 0 is not allowed. */
   /*    When add a Fixed Asset(Feature 2) or only modify the books cost (Feature 3) this field is mandatory. */
   /*   Err Msg: You must specify the book cost */
   find first ttFixedAssetBooks where (ttFixedAssetBooks.bookCost = 0 or ttFixedAssetBooks.bookCost = ?)
      and (ttFixedAssetBooks.operation = {&ADD}
      or ttFixedAssetBooks.operation = {&MODIFY}) no-lock no-error.
   if available ttFixedAssetBooks
   then do:
      {us/bbi/pxmsg.i &MSGNUM=16119 &ERRORLEVEL=3}
      l_error = yes.
   end.

   /* 14  If records exist in ttFixedAssetBooks table, then the bookButton must be "Adjust".
   ErrMsg: The field bookButton must contain "Adjust" */
   if can-find(first ttFixedAssetBooks where ttFixedAssetBooks.bookbutton <> 'Adjust')
   then do:
      MessageArg = "bookButton".
      MessageArg2 = "Adjust".
      {us/bbi/pxmsg.i &MSGNUM=16106 &ERRORLEVEL=3 &MSGARG1=MessageArg &MSGARG2=MessageArg2}
      l_error = yes.
   end.

   /* 15  Default exchange rates not found
   ErrMsg: Default exchange rates are not found in the system. */

   /* update book cost/salvage to header */
   for each ttFixedAssetMaster:
      for first ttFixedAssetBooks where ttFixedAssetBooks.faId = ttFixedAssetMaster.faId and
         ttFixedAssetBooks.fabFabkId = l_primary_book:

         ttFixedAssetMaster.faPuramt = ttFixedAssetBooks.bookCost.

         if ttFixedAssetBooks.bookSalvamt <> 0 and ttFixedAssetBooks.bookSalvamt <> ?
         then 
            ttFixedAssetMaster.faSalvamt = ttFixedAssetBooks.bookSalvamt.
         else do:
            {us/gp/gprunp.i "fapl" "p" "fa-get-salvage"
               "(input  ttFixedAssetMaster.faId,
                 input  l_primary_book,
                 output ttFixedAssetMaster.faSalvamt)"}.
         end.

      end.

   end.

end.

PROCEDURE ip-postdisplay:

   if c-application-mode <> "API"
   then
      color display message fa_id
      with frame f-1.

END PROCEDURE. /* ip-postdisplay */
